我必须为班级实现一个循环队列。当我测试它时,程序正确地入队和出队。但是每当我创建一个新对象并将其设置为另一个对象时,一切都会正确打印出来,但它最终会崩溃,并出现错误:
Expression: _BLOCK_TYPE_IS_VALID(pHead -> nBlockUse)
我运行了调试器,它说问题出在 Dequeue 函数的一行中。这是那个功能。
void CQUEUE::Dequeue()
{
if(Isempty())
{
cout << "Queue is empty, nothing to delete." << endl;
return;
}
if((!Isempty()) && (front == back)) //When there is only 1 item in the queue
{ // front and back will point to the same node
delete front; //but it wont be null because of that 1 item
front = back = 0;
return;
}
qnode *p = front;
front = front -> next;
delete p;//<----------DEBUGGER POINTS HERE**************
front -> prev = back;
return;
}
就像我说的,程序运行良好,直到我创建一个新对象并执行此操作
CQUEUE j = k;//Any Enqueues and Dequeues after this work, but it crashes
这是复制构造函数以防万一这是问题?
CQUEUE::CQUEUE(CQUEUE & original)//Will not compile if I put Const
{ //in the parameter for some reason
front = back = 0; //So I took it out
(*this) = original;
front -> prev = back;
back -> next = front;
}