链表“抛出异常”

问题描述 投票:-2回答:2

我正在为学校使用此链接列表程序,当我尝试运行程序时,收到此消息,提示“抛出异常:读取访问冲突,学生为nullptr”。这是我的主要功能是找到“学生”

int main() {
    ifstream datafile;
    list studentinfo;
    list::node *student = NULL;
    list::nodePtr head = NULL;
    list::node nodeinfo[10];

    datafile.open("Z:\\CS246\\Linked_List_Final\\studentdata.txt");
    if (!datafile)
    {
        cout << "Error!!";
        exit(1);
    }
    for (int i = 0; i < 10; i++) {
        datafile >> nodeinfo[i].name >> nodeinfo[i].id >> nodeinfo[i].gpa;
        studentinfo.addNode(&student, student->id);
    }
    list displayList(studentinfo);

    string choice, ans;
    int pos;

    cout << "Would you like to delete a node? If so type yes:";
    cin >> choice;
    if (choice == "yes" || choice == "Yes") {
        do {
            cout << "Type the postion of the node you would like to delete";
            cin >> pos;
            studentinfo.deleteNode(pos);
            cout << "Is there another node you would like to delete?";
            cin >> ans;
        } while (ans == "yes" || ans == "Yes");
    }

    system("pause");
    return 0;
}

[请让我知道是否需要发布其余代码。

c++ oop linked-list singly-linked-list
2个回答
0
投票
    studentinfo.addNode(&student, student->id);

在这里,您尝试取消引用student,即使其值为nullptr。在这样做之前,您需要为其分配一些值。也许您正在寻找nodeinfo[i].id吗?


0
投票
studentinfo.addNode(&student, student->id);

是这样,当您尝试访问它时(->)仍​​未实例化student吗?无法访问nullptr。

© www.soinside.com 2019 - 2024. All rights reserved.