我正在尝试使用 C++ 创建一个两侧队列。我正在使用 Visual Studio 2012 并不断获得:
First-chance exception at 0x00D95A29 in Console_Assignment1.exe: 0xC0000005: Access violation writing location 0x00000008.
我认为我遇到了指针问题(可能试图取消引用我不应该引用的东西)。到目前为止,我发现问题的运气为零,我真的很感激再看一眼。
(代码太长无法粘贴,所以我只复制我认为给我带来问题的函数。)也许只是一个小的概述。我有一个节点类,它包含两个指向节点的指针(下一个和上一个)和一个 int (值)。和一个队列类,它包含两个指向节点(第一个和最后一个)和一个 int(大小)的指针。
// enqueueBeg - adds a new node at the beginning of the queue.
void DBL_Queue::enqueueBeg(int insert_val)
{
node* new_node = new node(insert_val); // Creates the new node.
new_node->setNext( this->getFirst() ); // Connects the new node to the first in the queue
this->getFirst()->setPrev( new_node ); // Connects the first node in the queue to the new one
this->setFirst( new_node ); // Sets the new node as the first in the queue
this->setSize ( this->get_queue_size() + 1 ); // adds 1 to the size of the list
// dequeueBeg - removes the first node of the queue.
int DBL_Queue::dequeueBeg()
{
int ret_value = this->getFirst()->getVal();
node* old_node = this->getFirst();
this->setFirst( this->getFirst()->getNext() ); // Sets the second node in the queue as the first.
this->getFirst()->setPrev( NULL ); // Removes the link between the new first new and the old one.
this->setSize( this->get_queue_size() - 1); // Removes 1 from queue size
delete old_node; // Deletes the node that use to be first.
return ret_value; // Returns the value of the old node.
// DBL_Queue Destructor
DBL_Queue::~DBL_Queue()
{
if (this->first == NULL) // if queue is empty do nothing
return;
else
{
while (this->first->getNext() != NULL) // go through all nodes and delete them one by one
{
node* deletion = this->getFirst();
this->setFirst( this->getFirst()->getNext() );
delete deletion;
}
}
}
在此先感谢您的帮助!