gpt4 book ai didi

c++ - 将新节点添加到具有两个类的链表

转载 作者:行者123 更新时间:2023-11-30 05:32:09 24 4
gpt4 key购买 nike

当尝试将新的 Node 添加到喜欢的列表时,它给我一个 Segmentation fault。谁能告诉我 addBook() 函数的实现有什么问题。我不确定是函数的实现有问题,还是我声明类的方式有问题。

class Reservation {
public:

int getID();
string getResevNum();

void setId(int x);
void setReseNum(string y);

private:
int ID;
string reservedNumber;
};

class ReservationCollection {
public:
ReservationCollection();
~ReservationCollection();

int getUserId(int &id);
string getUserBook(string &bookCall);

void findReservation();
void display();

void addBook(int id, string book);
void RemoveBook();
void ShutDown();

private:
struct Node {
Reservation *data;
Node *next;
};
Node *head;
};

ReservationCollection::ReservationCollection() {
Node *head = new Node;
head->next = NULL;
}

ReservationCollection::~ReservationCollection() {
}

void ReservationCollection::addBook(int id, string book){
Node *tmp = new Node;

tmp->data->setId(id);
tmp->data->setReseNum(book);
tmp->next = head->next;
head->next = tmp;
cout <<"Good\n";
}

int Reservation::getID(){
return ID;
}

string Reservation::getResevNum(){
return reservedNumber;
}
void Reservation::setId(int x){
ID = x;
}

void Reservation::setReseNum(string y){
reservedNumber = y;
}

int ReservationCollection::getUserId(int &id){
cout << "Enter Id number " << endl;
cin >> id;
return id;
}

string ReservationCollection::getUserBook(string &bookCall){
cout << "Enter book reservatin " << endl;
cin >> bookCall;
return bookCall;
}

int main()
{
int ID;
string BookNum;
char cmd;

do {
cout << "Enter command: ";
cin >> cmd;

ReservationCollection list;
if (cmd == 'A' || cmd == 'a'){

list.getUserId(ID);
list.getUserBook(BookNum);

list.addBook(ID, BookNum);
}
else if (cmd == 'S' || cmd == 's'){
cout << " list";
}
} while (cmd != 'Q' || cmd == 'q');

}

最佳答案

希望我能评论一下:

Node *tmp = new Node; 行的所有实例都应显示为 Node *tmp = new Node();

关于c++ - 将新节点添加到具有两个类的链表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35248009/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com