【问题标题】:C++ Undefined references?C++ 未定义的引用?
【发布时间】:2013-01-08 21:14:17
【问题描述】:

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

"[Linker] 引用 Customer_Record()'" , [Linker error] undefined reference toCar_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 \n\n");
    printf("               Enter 2 to go to Car Record \n\n");
    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();
}

【问题讨论】:

  • void Car_Record(); 声明一个函数。
  • @chris:我认为这是本意
  • @aschepler,缺少编译器错误源于代码位于main 中。有一个函数声明,后跟一段代码。
  • @CarlNorum:你是一个活生生的编译器
  • 这里有很多错误。值得注意的是,您在 if 语句中使用 assignment 运算符而不是 comparison 运算符 ==.

标签: c++ dev-c++


【解决方案1】:

我已经编制了一份您的程序存在的所有问题的清单。这里是:

  • 您的 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 用于 iosteam。 getchar 的等价物是 std::cin.get()`。

  • Main 返回 int: 在标准 C++ 中,您不能省略返回类型。指定 main 的返回类型为int,但你不必把return 0放在最后(这是隐式的)。

如果您想查找 C++ 函数和容器(如 std::cout/std::cin),this reference 会很有帮助。

【讨论】:

    【解决方案2】:
    1. 您在 main 末尾缺少 },编译器认为您的函数声明在 main 函数中。

    2. 从函数中删除尾随分号。例如:

      void Car_Record();
      {   
      

       void Car_Record()
       {   
      

    那个分号不是必须的。

    【讨论】:

    • main 末尾有一个}。如果他有这样的语法问题,他就不会出现链接器错误。你对分号的看法是对的,除了它不仅没有必要,事实上它不能在那里。
    • 卡尔,好点子。当我阅读它时,getchar 调用只是与函数混合在一起。
    • 是的,原来的格式很糟糕,很难看。
    【解决方案3】:

    不要这样嵌套你的函数。 Customer_Record()Car_Record() 的定义应该在main()outside。您还需要从这些函数的定义中删除 ;

    尝试更好地格式化您的代码 - 从长远来看会有很大帮助。

    【讨论】:

    • 功能分离,取下;关闭了函数的定义,但我遇到了另一个错误“预期在“void”之前的 init-declarator 很抱歉,但我对此很陌生
    • 抱歉,没有看到新代码,这将很难诊断。您当然可以提出一个新问题来跟进。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-21
    • 2015-02-14
    • 2015-12-05
    • 1970-01-01
    • 2020-12-03
    • 2016-06-06
    相关资源
    最近更新 更多