有错误"Undefined symbols for architecture x86_64"和"ld: symbol(s) not found for architecture x86_64"



我是编程新手,学习了我的第一个C++类。我有一项作业,正在尝试编写一个C++代码,用于创建一个学生链接列表,操作选择为"INSERT、UPDATE、DELETE、SHOW、write"。我以为我已经写完了代码,因为当我写完的时候,我没有任何错误。然而,当我尝试运行时,"构建失败",并在图像上看到错误。这是我第一次看到这种类型的错误。如果有人帮我弄清楚为什么会发生这种事,以及如何解决,我会非常高兴。非常感谢。

我的代码如下;

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define TRUE 1
#define FALSE 0
struct person {
    int id;
    char name [20];
    float grade;
    struct person *next ;
}Student;
typedef struct person *StudentPtr;
StudentPtr slist = NULL;
int ItemFound = FALSE;
int ItemExists = FALSE;

void GetStudent (StudentPtr *TemPtr)
{
    *TemPtr = (StudentPtr)malloc(sizeof(person));
}

void FindStudent (StudentPtr *PredPtr, StudentPtr *LocPtr, int id)
{
    *PredPtr = NULL;
    *LocPtr = slist;
    ItemFound = FALSE;
    ItemExists = FALSE;
    while(ItemFound == FALSE && (*LocPtr!=NULL))
    {
        if((*LocPtr)->id < id)
        {
            *PredPtr = *LocPtr;
            *LocPtr = ((*LocPtr)->next);
        }
        else
        {
            ItemFound = TRUE;
            if(((*PredPtr)->next)->id == id)
                ItemExists = TRUE;
        }
    }
}
void AddStudent(int id, char name)
{
    StudentPtr Something, PredPtr, TemPtr;
    FindStudent(&PredPtr, &Something, id);
    GetStudent(&TemPtr);
    TemPtr->id = id;
    TemPtr->grade = 0.0;
    strcat(TemPtr->name, &name);
    if(ItemExists == TRUE)
        printf("Student is already in the list.");
    else
    {
        if(PredPtr == NULL)
    {
        TemPtr->next = slist;
        slist = TemPtr;
        printf("[%d, %s] Adding operation is SUCCESSFUL to the linked list. Student order: ", id, &name);
    }
    else
        TemPtr->next = PredPtr->next;
        PredPtr->next = TemPtr;
        printf("[%d, %s] Adding operation is SUCCESSFUL to the linked list. Student order: ", id, &name);
    }
}
void UpdateStudent(int id, float grade)
{
    StudentPtr Something, PredPtr;
    FindStudent(&PredPtr, &Something, id);
    if(ItemExists == FALSE)
        printf("[%f] Student does not found! Grade is not changed.]", grade);
    else
        (PredPtr->next)->grade = grade;
}
void DeleteStudent(int id)
{
    StudentPtr LocPtr, PredPtr;
    FindStudent(&PredPtr, &LocPtr, id);
    if(ItemExists == FALSE)
        printf("[%d] Student does not found! Delete operation is not completed.", id);
    else
    {   if(PredPtr == NULL)
        {   slist = LocPtr->next;
            free(LocPtr);
            printf("[%d] Delete operation is SUCCESSFUL.", id);
        }
        else
        {
            PredPtr->next = LocPtr->next;
            free(LocPtr);
            printf("[%d] Delete operation is SUCCESSFUL.", id);
        }
    }
}
int main()
{
    int ENDED=FALSE;
    while(ENDED==FALSE)
    {
        printf("Welcome to Student Registration Program!nn");
        printf("[1] Insert, [2] Change, [3] Remove, [4] Show, [5] Write, [9] to exitnn");
        int selection;
        scanf("%d", &selection);
        printf("nSelect: %dnn", selection);
        if(selection==1 || selection==2 || selection==3 || selection==4 || selection==5 || selection==9)
        {
            int id;
            switch(selection)
            {
                case 1:
                {   char name[40];
                    char surname[40];
                    printf("nPlease input id, name, surname: ");
                    scanf("%d %s %s", &id, name, surname);
                    strcat(name, " ");
                    strcat(name, surname);
                    void AddStudent(int, char[40]);
                    AddStudent(id, name);
                    break;
                }
                case 2:
                {   float grade;
                    printf("nPlease input id, grade: ");
                    scanf("%d %f", &id, &grade);
                    UpdateStudent(id, grade);
                    break;
                }
                case 3:
                {   printf("nPlease input id: ");
                    scanf("%d", &id);
                    DeleteStudent(id);
                    break;
                }
                case 4:
                {   StudentPtr LocPtr = slist;
                    printf("n        Id             Name   Grade");
                    printf("n----------   --------------   -----");
                    while(LocPtr != NULL)
                    {
                        printf("n%10d %16s %7.2f", LocPtr->id, LocPtr->name, LocPtr->grade);
                        LocPtr = LocPtr->next;
                    }
                    printf("nPlease press a button to continue...");
                    char button;
                    scanf("%c", &button);
                    break;
                }
                case 5:
                {   FILE *fout = fopen("Students.txt","w");
                    StudentPtr temp;
                    temp = slist;
                    fprintf(fout, "n        Id             Name   Grade");
                    fprintf(fout, "n----------   --------------   -----");
                    while(temp != NULL)
                    {
                        printf("n%10d %16s %7.2f", temp->id, temp->name, temp->grade);
                        temp = temp->next;
                    }fclose(fout);
                    printf("nAll records have been written to the file.");
                    printf("nPlease press a button to continue...");
                }
                case 9:
                {    printf("Thanks for using this program. See you later.");
                    ENDED = TRUE;
                }
            }
        }
        else
            printf("Please enter a valid selection.");

    }
}

简短回答:您尚未定义名为AddStudent的函数,该函数的参数类型为:(int, char*)。相反,您使用类型为(int, char)的参数定义了一个。注意,char*是一个字符串,而char只是一个符号/字节。

长答案:你知道你还没有真正写过C++类。事实上,您已经编写了一个包含person数据的结构,在变量名Student下创建了该结构的一个实例,并且您有一组使用该结构的函数。这绝对不是C++类!

相反,你应该写这样的东西:

class Person {
public:
    int id;
    char* name;
    float grade;
    Person *p_next;
}
class Students {
public:
    void    addStudent(int id, char* name);
    Person* getStudent(int id);
private:
    Person *p_init;
}

在本例中,Person是底层结构,Students表示链表处理程序。如果你想更新学生的成绩,你可以做:

Students students;
students.addStudent(...);
Person *student = students.getStudent(id);
student->grade = 22;

您没有定义AddStudent(int id,char*name)。您的定义是AddStudent(int id,char name)。您应该将其更改为AddStudent(int id,char*name)。

相关内容

  • 没有找到相关文章

最新更新