I am trying to create a two-way queue with C ++. I am using Visual Studio 2012 and keep getting:
First-chance exception at 0x00D95A29 in Console_Assignment1.exe: 0xC0000005: Access violation writing location 0x00000008.
I think I have a problem with the pointer (maybe you are trying to dereference something that I shouldn't). I’m still unlucky to find this problem, and I really appreciate the second look.
(The code is too long to insert, so I just copy functions that I think give me a problem.) Maybe just a short review. I have a node class that contains two pointers to node (next and previous) and int (value). and a queue class containing two pointers to node (first and last) and int (size).
// 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; } } }
Thanks in advance for your help!
source share