当我在键入float之后运行addRecord((时,我一直会遇到分段错误(核心转储(。我在函数中使用指针可能有严重问题,但我似乎找不到问题所在。这是我为学校做的一个项目。别担心,截止日期已经过去了。我只想知道哪里出了问题,因为这让我发疯了。有人请帮忙。
struct employee{
double salary;
string name;
student* next;
};
struct linkedlist{
employee *head=NULL;
employee *tail=NULL;
};
struct linkedlist list;
void addRecord(){
string name;
cout << "Type the name of the employee:";
cin >> name;
float salary;
cout << "Type the final average of the employee:";
cin >> salary;
struct employee *newOne;
struct employee *head=list.head;
newOne->name=name;
newOne->salary=salary;
newOne->next=NULL;
if(list.head==NULL && list.tail==NULL){
list.head=newOne;
list.tail=newOne;
}
else{
newOne->next = head;
list.head=newOne;
}
}
正如注释所说,您需要更改行:
struct employee *newOne;
实际创建新员工:
struct employee *newOne = new employee();
否则指针实际上不会指向真正的雇员结构,它就会崩溃。