我是编程新手,参加我的第一个C ++课程。我有一个作业,并尝试编写一个C ++代码,用于创建学生的链接列表,其中包含操作选择" INSERT,UPDATE,DELETE,SHOW,WRITE"。我以为我写完代码了,因为当我完成时我没有错误。但是,当我试图运行时," Build Failed"并在image上看到错误。这是我第一次看到这种类型的错误。如果有人帮助我弄清楚为什么会这样,以及如何解决它,我将非常高兴。非常感谢你。
我的代码正在关注;
#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!\n\n");
printf("[1] Insert, [2] Change, [3] Remove, [4] Show, [5] Write, [9] to exit\n\n");
int selection;
scanf("%d", &selection);
printf("\nSelect: %d\n\n", 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.");
}
}
答案 0 :(得分:0)
您没有定义AddStudent(int id,char * name)。 您的定义是AddStudent(int id,char name)。 您应该将其更改为AddStudent(int id,char * name)。
答案 1 :(得分:0)
简答:
您尚未定义名为AddStudent
的函数,该函数具有类型为(int, char*)
的参数。相反,您已使用类型为(int, char)
的参数定义了一个。请注意,char*
是一个字符串,而char
只是一个符号/字节。
答案很长:
你知道你还没有写过C++
课程。实际上,您已经编写了一个包含person
数据的结构,在变量名Student
下创建了该结构的单个实例,并且您有一堆与该结构一起使用的函数。这绝对不是一个C ++ CLASS!
相反,你应该写一些像:
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;