C ++:当我想输入' q'时,为什么这会给我一个异常抛出的错误退出?

时间:2018-04-12 23:47:07

标签: c++

抱歉,我不想被指控再次作弊lol

2 个答案:

答案 0 :(得分:2)

您的析构函数代码正在删除错误的节点。改为:

while (temp != NULL)
{
    QueueNodePtr next = temp->link;
    delete temp;
    temp = next;
}

答案 1 :(得分:1)

您的Queue析构函数未正确循环遍历节点。

在第一次循环迭代中,您使temp跳过第一个节点并指向第二个节点,然后您delete第二个节点并且不更新temp,所以temp现在指向第二次循环迭代中的无效节点。

请改为尝试:

Queue::~Queue()
{
    //create new pointer and point it to front of list
    QueueNodePtr temp = front;

    //while list is not empty
    while (temp != NULL)
    {
        //point to next node and delete the current node
        QueueNodePtr next = temp->link;
        delete temp;
        temp = next;
    }
}

或者这个:

Queue::~Queue()
{
    //create new pointer and point it to front of list
    QueueNodePtr temp = front;

    //while list is not empty
    while (temp != NULL)
    {
        //point to next node and delete the current node
        QueueNodePtr curr = temp;
        temp = temp->link;
        delete curr;
    }
}