c中用于用户定义函数的未解析外部符号

Unresolved external symbol in c for user-define function

提问人:Dewn 提问时间:6/19/2021 更新时间:6/19/2021 访问量:98

问:

我对编程非常陌生,在我的课程中,我们学习 C 语言,所以请怜悯我写得不好的代码,编译器会因为错误而无法运行程序,所以我很难确定出了什么问题。回到主要问题,我得到未解决的外部符号......在function_main中引用,我真的不知道我在哪里搞砸了

#include<stdio.h>
char DisplayMenu(void);
void ViewBooks();
void SearchBookPrice();
void UpdateBookPrice();


int main()
{
    char userchoice;
    int bookID[5] = { 200,230,500,540,700 };
    char bookTitle[5][50] = { {"Using Information Technology 12th Edition" }, { "Beyond HTML" },{"Build Your Own PC"},{"Instant Java Servlets"},{"DIgital Image: A Practical Guide"} };
    float bookPrice[5] = { 100.30,50.40,47,83.30,22.90 };

    do
    {
        userchoice = DisplayMenu();
        if (userchoice == 'V')
            ViewBooks();
        else if (userchoice == 'S')
            SearchBookPrice();
        else if (userchoice == 'U')
            UpdateBookPrice();
    } while (userchoice != 'E');

    printf("Thank you for using our system. Have a nice day!\n");

    return 0;
}

char DisplayMenu(void)
{
    char choice;

    printf("*************************************************************************\n");
    printf("V:View Books  S:Search Book Price  U:Update Book Price  E:Exit");
    printf("*************************************************************************\n");
 
    do
    {
        printf("Enter your choice: ");
        scanf(" %c", &choice);

        if (choice != 'V' && choice != 'S' && choice != 'U' && choice != 'E')
            printf("Invalid Choice\n");

    } while (choice != 'V' && choice != 'S' && choice != 'U' && choice != 'E');
    

    return choice;
}

void ViewBooks(int bookID[],float bookPrice[])
{
    printf("%d   Using Information Technology 12th Edition   $%f\n", bookID[0], bookPrice[0]);
    printf("%d   Beyond HTML   $%f\n", bookID[1], bookPrice[1]);
    printf("%d   Build Your Own PC   $%f\n", bookID[2], bookPrice[2]);
    printf("%d   Instant Java Servlets  $%f\n", bookID[3], bookPrice[3]);
    printf("%d   Digital Image: A Pratical Guide  $%f\n", bookID[4], bookPrice[4]);

    return;
}

void SearchBookPrice(int bookID[5],char bookTitle[5][50], float bookPrice[5])
{
    int idsearch, i= 0, match = -1;
    printf("*************************************************************************\n");
    printf("Search by book ID\n");
    printf("*************************************************************************\n");

    printf("Enter book ID: ");
    scanf("%d", &idsearch);

    while (i<5 && match==-1)
    {
        if (bookID[i] == idsearch)
            match = i;
        i++;
    }



    if (match == -1)
        printf("Please refer to the customer service for assitance");
    else
    {
        printf("The book id is : %d\n", &idsearch);
        printf("The price of %s is %f", bookTitle[match], bookPrice[match]);
    }

    return;

}

void UpdateBookPrice(int bookID[5], char bookTitle[5][50], float bookPrice[5])
{
    int idsearch, i = 0, match = -1;
    printf("Enter book ID: ");
    scanf("%d", &idsearch);
    while (i < 5 && match == -1)
    {
        if (bookID[i] == idsearch)
            match = i;
        i++;
    }
    if (match == -1)
        printf("The book ID is not found. Please make sure the book ID is correct");
    else
    {
        printf("Current book price is %f\n", bookPrice[match]);
        printf("Enter new price for (%d-%s): ", bookID[match], bookTitle[match]);
        scanf("%f", &bookPrice[match]);
    }
    return;
}
C 函数 未解析 - 外部

评论

1赞 Eugene Sh. 6/19/2021
发布完整的错误消息。您省略了最关键的信息 - 未解析的符号名称。
1赞 Eugene Sh. 6/19/2021
不要将任何参数传递给需要参数的函数。您可以通过提供带有参数列表的适当函数原型来在编译器级别防止这种情况。
1赞 Perette 6/19/2021
使用 C 编译器,它对我来说编译得很好。使用 C++ 编译器时,存在未解析的符号,因为您的前向声明和对 的调用不包含参数,这些参数是 C++ 中函数签名的一部分。它试图调用一个不带参数的版本,但你只提供了需要参数的版本。ViewBookSearchBookPriceUpdateBookPrice
1赞 Weather Vane 6/19/2021
如果 C 编译器没有警告错误,则应提高警告级别。不过,可执行代码确实链接了,没有任何未解析的外部符号。printf("The book id is : %d\n", &idsearch);
1赞 HAL9000 6/19/2021
如果你声明一个struct book { .... };

答:

0赞 Denis Turgenev 6/19/2021 #1

函数声明与函数体不匹配。

这是工作代码。

#include<stdio.h>
char DisplayMenu(void);
void ViewBooks(int bookID[],float bookPrice[]);
void SearchBookPrice(int bookID[5],char bookTitle[5][50], float bookPrice[5]);
void UpdateBookPrice(int bookID[5], char bookTitle[5][50], float bookPrice[5]);


int main()
{
    char userchoice;
    int bookID[5] = { 200,230,500,540,700 };
    char bookTitle[5][50] = { {"Using Information Technology 12th Edition" }, { "Beyond HTML" },{"Build Your Own PC"},{"Instant Java Servlets"},{"DIgital Image: A Practical Guide"} };
    float bookPrice[5] = { 100.30,50.40,47,83.30,22.90 };

    do
    {
        userchoice = DisplayMenu();
        if (userchoice == 'V')
            ViewBooks(bookID, bookPrice);
        else if (userchoice == 'S')
            SearchBookPrice(bookID, bookTitle, bookPrice);
        else if (userchoice == 'U')
            UpdateBookPrice(bookID, bookTitle, bookPrice);
    } while (userchoice != 'E');

    printf("Thank you for using our system. Have a nice day!\n");

    return 0;
}

char DisplayMenu(void)
{
    char choice;

    printf("*************************************************************************\n");
    printf("V:View Books  S:Search Book Price  U:Update Book Price  E:Exit");
    printf("*************************************************************************\n");
 
    do
    {
        printf("Enter your choice: ");
        scanf(" %c", &choice);

        if (choice != 'V' && choice != 'S' && choice != 'U' && choice != 'E')
            printf("Invalid Choice\n");

    } while (choice != 'V' && choice != 'S' && choice != 'U' && choice != 'E');
    

    return choice;
}

void ViewBooks(int bookID[],float bookPrice[])
{
    printf("%d   Using Information Technology 12th Edition   $%f\n", bookID[0], bookPrice[0]);
    printf("%d   Beyond HTML   $%f\n", bookID[1], bookPrice[1]);
    printf("%d   Build Your Own PC   $%f\n", bookID[2], bookPrice[2]);
    printf("%d   Instant Java Servlets  $%f\n", bookID[3], bookPrice[3]);
    printf("%d   Digital Image: A Pratical Guide  $%f\n", bookID[4], bookPrice[4]);

    return;
}

void SearchBookPrice(int bookID[5],char bookTitle[5][50], float bookPrice[5])
{
    int idsearch, i= 0, match = -1;
    printf("*************************************************************************\n");
    printf("Search by book ID\n");
    printf("*************************************************************************\n");

    printf("Enter book ID: ");
    scanf("%d", &idsearch);

    while (i<5 && match==-1)
    {
        if (bookID[i] == idsearch)
            match = i;
        i++;
    }



    if (match == -1)
        printf("Please refer to the customer service for assitance");
    else
    {
        printf("The book id is : %d\n", &idsearch);
        printf("The price of %s is %f", bookTitle[match], bookPrice[match]);
    }

    return;

}

void UpdateBookPrice(int bookID[5], char bookTitle[5][50], float bookPrice[5])
{
    int idsearch, i = 0, match = -1;
    printf("Enter book ID: ");
    scanf("%d", &idsearch);
    while (i < 5 && match == -1)
    {
        if (bookID[i] == idsearch)
            match = i;
        i++;
    }
    if (match == -1)
        printf("The book ID is not found. Please make sure the book ID is correct");
    else
    {
        printf("Current book price is %f\n", bookPrice[match]);
        printf("Enter new price for (%d-%s): ", bookID[match], bookTitle[match]);
        scanf("%f", &bookPrice[match]);
    }
    return;
}

评论

0赞 Weather Vane 6/19/2021
你确定这是“工作代码”吗?printf("The book id is : %d\n", &idsearch);
0赞 Dewn 6/19/2021
显然它现在不起作用,我可以运行代码,但通过将 idsearch 替换为 bookID[match] 来修复它
0赞 user3629249 6/20/2021
这个答案没有干净地编译!除其他事项外:“Untitled2.c:87:35:警告:格式'%d'需要类型为'int'的参数,但参数2的类型为'int *' [-Wformat=]”以及有关值更改的几个警告,因为: 正在尝试使用值初始化数组。注意:浮点值的尾随如下:float bookPrice[5] = { 100.30,50.40,47,83.30,22.90 };floatdoublef100.30f