当我在键入浮点数之后运行 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();
否则指针实际上不会指向一个真正的员工结构,它会崩溃。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句