簡體   English   中英

C ++未定義的引用?

[英]C++ Undefined references?

所以這是我的程序的一部分,我無法調用函數,我真的需要一些幫助。 它基本上是選擇任一功能並輸入數據,然后打印該數據。 我做錯了什么?請幫助,我一直在努力

“[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();
}

不要像這樣嵌套你的功能。 Customer_Record()Car_Record()的定義應該 main() 你需要采取; 這些功能的定義也是如此。

嘗試更好地格式化代碼 - 從長遠來看,這將有很大幫助。

  1. 你在main的末尾錯過了一個},編譯器認為你的函數聲明在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標頭已經擁有的。 例如,iosteam的std::coutstd::cin 相當於getchar是std :: cin.get()`。

  • Main返回int:在標准C ++中,您不能忽略返回類型。 將main的返回類型指定為int ,但不必在末尾放置return 0 (這是隱式的)。

如果你想查找C ++函數和容器(比如std::cout / std::cin ), 這個引用有很多幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM