dev C++ 未定义的引用



所以这是我程序的一部分,我在调用函数时遇到问题,我真的需要一些帮助。基本上是选择任一功能并输入数据和 稍后打印该数据。 做错了什么?请帮忙,我一直得到

"[Linker] 对 Customer_Record()'" , [Linker error] undefined reference to Car_Record() ' 和"ld 返回 1 个退出状态"的引用"

 #include <stdio.h>
 #include <string.h>  
 #include <stdlib.h>
 #include <windows.h>  
 #include <conio.h>
void Customer_Record(), Car_Record();
int num;
struct Customer {
    char customer_ID[20];
    int license;
    char address[20];
    int phone;
    char email[20];
} cust;
struct car {
    int regno[20];
    char model[20];
    char colour[10];
} car;

main() {
    printf("               Enter 1 to go to Customer Record nn");
    printf("               Enter 2 to go to Car Record nn");
    scanf("%d", &num);
    if (num = 1) {
        Customer_Record();
    } else if (num = 2) {
        Car_Record();
    } else {
        printf("Invalid Number");
    }
    system("cls");
    void Customer_Record(); {
        printf("********CUSTOMER RECORD********"); /* accepts into*/
        printf("nEnter the name of the customer ");
        scanf("%s", &cust.customer_ID);
        printf("Enter the license number of the customer ");
        scanf("%d", &cust.license);
        printf("Enter the address of the customer ");
        scanf("%s", &cust.address);
        printf("Enter the cell phone number of the customer ");
        scanf("%d", &cust.phone);
        printf("Enter the email address of the customer ");
        scanf("%s", &cust.email);
    }
    void Car_Record(); {
        printf("********CAR RECORD********");
        printf("nEnter the car's registration number ");
        scanf("%d", &car.regno);
        printf("Enter the car's model ");
        scanf("%s", &car.model);
        printf("Enter the colour of the car ");
        scanf("%s", &car.colour);
    }
    getchar();
    getchar();
}

不要像那样嵌套你的函数。 Customer_Record()Car_Record()的定义应该在main()之外。 您还需要从这些函数的定义中解脱;

尝试更好地格式化代码 - 从长远来看,这将有很大帮助。

  1. 你在 main 的末尾缺少一个 },编译器认为你的函数声明在主函数内。

  2. 从函数中删除尾随分号。前任:

    void Car_Record();
    {   
    

     void Car_Record()
     {   
    

该分号不是必需的。

我已经编制了一个程序所有问题的列表。在这里:

  • if 语句使用赋值=运算符,而它们应该使用比较运算符==。 例如,更改以下内容:

    if (num = 1) {
    

    if (num == 1) {
    

    这也出现在您的else语句中。

  • 在 main 内部定义函数也是不正确的。必须在主子句之外定义这些块。您已经在 main 上方对函数进行了原型设计,现在您必须在 main 下面定义它们。此外,定义函数时,参数列表后不应有分号;这在语法上是错误的。

以下是建议。您正在编译此代码C++但这是使用 C 函数/标头编写的。要将其转换为C++,请执行以下更改:

  • 更改你的标题:stdio.h,conio.h,stdlib.h;这些都是C样式的标题。基本上,所有以".h"结尾的标头都是 C 样式的标头。C++有自己的 I/O 库,因此使其 C 等效项过时。请改为包含以下标头:

    #include <iostream>
    #include <cstdlib>
    

    我省略了额外的标头,因为您似乎只使用 printf/scanfsystem ,相当于 C++ 的 iostreamcstdlib 标头已经拥有的标头。例如,std::coutstd::cin for iosteam。相当于getchar是std::cin.get()'。

  • 主返回 int:在标准C++中,不能省略返回类型。将 main 的返回类型指定为 int ,但不必将return 0放在末尾(这是隐式的)。

如果要查找C++函数和容器(如std::cout/std::cin),此参考有很大帮助。

最新更新