简体   繁体   English

C ++未定义的引用?

[英]C++ Undefined references?

So this is a segment of my program, i am having trouble calling the the functions and I really need some help. 所以这是我的程序的一部分,我无法调用函数,我真的需要一些帮助。 It's basically to choose either function and enter data and later print that data. 它基本上是选择任一功能并输入数据,然后打印该数据。 What am doing wrong ?please help, I keep getting 我做错了什么?请帮助,我一直在努力

"[Linker] reference to Customer_Record()'" , [Linker error] undefined reference to Car_Record() ' and "ld returned 1 exit status" “[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 \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();
}

Don't nest your functions like that. 不要像这样嵌套你的功能。 The definitions of Customer_Record() and Car_Record() should be outside of main() . Customer_Record()Car_Record()的定义应该 main() You need to take the ; 你需要采取; off of the definitions of those functions, too. 这些功能的定义也是如此。

Try formatting your code better - that will help a lot in the long run. 尝试更好地格式化代码 - 从长远来看,这将有很大帮助。

  1. You're missing a } at the end of main, the compiler thinks your function declarations are within the main function. 你在main的末尾错过了一个},编译器认为你的函数声明在main函数内。

  2. Remove the trailing semicolon from your function. 从函数中删除尾随分号。 Ex: 例如:

     void Car_Record(); { 

    to

      void Car_Record() { 

That semicolon isn't necessary. 分号不是必需的。

I've compiled a list of all the problems with your program. 我编制了一个包含程序所有问题的列表。 Here it is: 这里是:

  • Your if statements are using the assignment = operator when they should be using the comparison operator == . if语句在使用比较运算符==时使用赋值=运算符。 So for example, change the following: 例如,更改以下内容:

     if (num = 1) { 

    to

     if (num == 1) { 

    This also occurs in your else statement as well. 这也发生在你的else语句中。

  • It is also incorrect to define functions inside of main. 在main中定义函数也是不正确的。 You must define these blocks outside the main clause. 您必须在主要子句之外定义这些块。 You've already prototyped the functions above main, now you must define them below main. 你已经将main上面的函数原型化了,现在你必须在main下面定义它们。 In addition, you shouldn't have a semicolon after the parameter list when defining functions; 另外,在定义函数时,参数列表后面不应该有分号; that is syntactically wrong. 这在语法上是错误的。

The following is advice. 以下是建议。 You're compiling this code as C++ but this is written using C functions/headers. 您正在将此代码编译为C ++,但这是使用C函数/头编写的。 In order to convert it to C++, do the following changes: 要将其转换为C ++,请执行以下更改:

  • Change your headers: stdio.h, conio.h, stdlib.h; 更改标题: stdio.h,conio.h,stdlib.h; these are all C-style headers. 这些都是C风格的标题。 Essentially all headers that end in ".h" are C-style headers. 基本上所有以“.h”结尾的标题都是C风格的标题。 C++ has its own I/O library, thus making its C equivalent obsolete. C ++有自己的I / O库,因此它的C等效过时。 Include the following headers instead: 改为包括以下标题:

     #include <iostream> #include <cstdlib> 

    I left out additional headers because it seems you're only using printf / scanf and system , the equivalent of which C++'s iostream and cstdlib headers already possesses. 我遗漏了额外的标题,因为它似乎只使用了printf / scanfsystem ,相当于C ++的iostreamcstdlib标头已经拥有的。 For example, std::cout and std::cin for iosteam. 例如,iosteam的std::coutstd::cin And the equivalent of getchar is std::cin.get()`. 相当于getchar是std :: cin.get()`。

  • Main returns int: In standard C++, you cannot elide the return type. Main返回int:在标准C ++中,您不能忽略返回类型。 Specify the return type of main as int , but you don't have to put return 0 at the end (this is implicit). 将main的返回类型指定为int ,但不必在末尾放置return 0 (这是隐式的)。

If you want to look up C++ functions and containers (like std::cout / std::cin ), this reference helps a lot. 如果你想查找C ++函数和容器(比如std::cout / std::cin ), 这个引用有很多帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM