最近几天我第一次弄储存对象的链表
然后遇到了这个问题,非常难受
问题主要在这里
报错是这个
如果改成这样
也会报错
我人傻了
附上源代码。。。
#include <iostream>
#include <string>
#include <stdio.h>
using namespace std;
//学生类
class student {
private:string id;
private:string name;
private:string sex;
private:string height;
private:string score;
public:void setter(
string id1 = " ",
string name1 = " ",
string sex1 = " ",
string height1 = " ",
string score1 = " ") {
this->id = id1;
this->name = name1;
this->sex = sex1;
this->height = height1;
this->score = score1;
}
public:string getId() {
return this->id;
}
public:string getName() {
return this->name;
}
public:string getSex() {
return this->sex;
}
public:string getHeight() {
return this->height;
}
public:string getScore() {
return this->score;
}
public:student(
string id = " ",
string name = " ",
string sex = " ",
string height = " ",
string score1 = " "
){
this->id = id;
this->name = name;
this->sex = sex;
this->height = height;
this->score = score;
}
};
//链表的结构体模块
struct node{
int n;
student stu;
struct node* next;
};
//链表添加模块
void linkList_add(student stu1,struct node *p1,struct node** p0,int n) {
p1 = (struct node*)malloc(sizeof(struct node));
p1->n = n;
//对这里面的对象进行复制
p1->stu.setter(stu1.getId(),stu1.getName(),stu1.getSex(),stu1.getHeight(),stu1.getScore());
p1->next = NULL;//每一个新结点暂时都当成是最后一个结点
(*p0)->next = p1; //把头结点的指针指向本新结点
(*p0) = (*p0)->next;//把p0后移一个结点,为下一个新结点连进来做准备
}
void ListOut(struct node* head){
while (head->next != NULL) {//表示链没有结束
head = head->next;
cout << head->n;
}
}
int main() {
struct node* head; //头结点
head = (struct node*)malloc(sizeof(struct node));//开辟一个新结点,让head指向它
struct node* p1= NULL;//用于指向有效数据的新结点
struct node* p0 = head;//让p0始终指向新结点的前一个结点,它与p1同步向后移
int count = 1;
string ifcon = " ";
student stu;//这是一个用于缓存的student对象
string id, name, sex, height, score;
int no;
cin >> no;
switch(no) {
case 1: {
while (1) {
cout << "输入'开始/继续'录入学生数据,输入'结束'停止录入" << endl;
cin >> ifcon;
if (ifcon == "结束") {
break;
}
else if (ifcon == "开始" || ifcon == "继续") {
cout << "请输入第" << count << "个学生的学号,姓名,性别,身高,入校成绩" << endl;
cin >> id >> name >> sex >> height >> score;
stu.setter(id, name, sex, height, score);
linkList_add(stu,p1,&p0,114514);
linkList_add(stu, p1, &p0, 1919810);
}
else {
cout << "输入的指令无效" << endl;
}
}
break;
};
case 2:;
case 3:;
case 4:;
case 5:;
}
ListOut(head);
}