使用两个类

时间:2016-02-06 23:53:37

标签: c++

尝试将新的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');

}

3 个答案:

答案 0 :(得分:1)

希望我能对此发表评论:

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

答案 1 :(得分:0)

创建节点不会创建Reservation *数据的结构;试图指向,所以你试图访问非初始化的内存。 您需要初始化数据: tmp-&gt; data = new Reservation();

答案 2 :(得分:0)

如果你不必,不要使用指针。在这个例子中,Node必须是一个指针,因为列表可能有零节点或1000个节点,所以我们需要指针按需动态分配内存。

但是(Reservation)data不一定是指针。每个节点始终有一个Reservation成员。

如果你确实将它声明为指针,那么你必须分配它,并在不再需要它时释放它。

head应初始化为NULL,因为单个链接列表在初始化时没有节点。插入的第一个节点成为头部。

#include <iostream>
#include <string>

using namespace std;

class Reservation 
{
private:
    int ID;
    string reservedNumber;
public:

    int getID() { return ID; }
    string getResevNum() { return reservedNumber; }

    void setId(int x) { ID = x; }
    void setReseNum(string y) { reservedNumber = y; }
};

class ReservationCollection
{
public:
    struct Node
    {
        Reservation data;
        Node *next;
    };

    ReservationCollection();
    ~ReservationCollection();

    void addBook(int id, string book);

    Node* getHead() { return head; }

private:
    Node *head;
};

ReservationCollection::ReservationCollection()
{
    head = NULL;
}

ReservationCollection::~ReservationCollection()
{
    Node *p = head;
    while (p)
    {
        Node *next = p->next;
        cout << "delete: " << p->data.getID() << ", " << p->data.getResevNum() << endl;
        delete p;
        p = next;
    }
}

void ReservationCollection::addBook(int id, string book)
{
    Node *node = new Node;
    node->data.setId(id);
    node->data.setReseNum(book);

    //this element is the last element
    node->next = NULL;

    if (!head)
    {
        //first element inserted
        head = node;
        head->next = NULL;
    }
    else
    {
        //find the previous node in the list
        Node *prev = head;
        while (prev->next)
            prev = prev->next;

        //this node is after previous node
        prev->next = node;
    }

    cout << "Good\n";
}

int main()
{
    ReservationCollection list;
    list.addBook(0, "Book0");
    list.addBook(1, "Book1");
    list.addBook(2, "Book2");
    list.addBook(3, "Book3");

    ReservationCollection::Node *p = list.getHead();

    while (p)
    {
        ReservationCollection::Node *next = p->next;
        cout << p->data.getID() << ", " << p->data.getResevNum() << endl;
        p = next;
    }
}