Note that there are some explanatory texts on larger screens.

plurals
  1. POCannot dequeue a node in C++
    primarykey
    data
    text
    <p>My query: why does my program come up with a run-time error when dequeuing (actually, deleting) the only node I've enqueued in the queue? I've written some debugging statements in the appropriate function, which points out that there is something wrong with the line</p> <blockquote> <p>delete front;</p> </blockquote> <p>When the program executes the line, it wants to quit because it does not respond to me. I tried </p> <blockquote> <p>if(front) delete front;</p> </blockquote> <p>but to no avail. I tried to find an answer on Google but did not come up with any satisfactory results. This is very strange. I've been dealing with OOP for a few years now, but that's a first for me. Here is my code:</p> <p>================================ Queue.cpp =======================================</p> <pre><code>#include &lt;iostream&gt; #include "Queue.h" using namespace std; Queue::Queue() { QueueNode * front = NULL; QueueNode * rear = NULL; } Queue::~Queue() { while(rear) dequeue(); } void Queue::enqueue(int row, int col) { // create the child state: QueueNode * newNode; newNode = new QueueNode; // write in child state's coordinates: newNode-&gt;row = row; newNode-&gt;col = col; // enqueue the child state: if(!front) // if empty, new is front { // first node = front and back front = newNode; rear = newNode; } else // not the first node: { newNode-&gt;next = rear; // new points to back rear = newNode; // new is the back } } void Queue::dequeue() { cout &lt;&lt; "\nHere\n"; delete front; cout &lt;&lt; "\nHere 2\n"; front = rear; cout &lt;&lt; "\nHere 3\n"; while(front-&gt;next) front = front-&gt;next; cout &lt;&lt; "\nHere 4\n"; } </code></pre> <p>================================= Queue.h =======================================</p> <pre><code>#ifndef QUEUE_H #define QUEUE_H class Queue { public: struct QueueNode { // numbers: int row; int col; QueueNode * next; }; QueueNode * front; QueueNode * rear; Queue(); ~Queue(); void enqueue(int, int); void dequeue(); //void traverse(); // scan for repetition of location. //void displayQueue() const; }; #endif </code></pre> <p>Note: </p> <p>1) I did not include the code for the main driver because that would take a lot of substitution of tabs for 4 spaces. </p> <p>2) I've enqueued only one queue node.</p> <p>3) I've made everything public in the queue class because I was desperate in finding out what the problem is. I'm going to keep it that way for the time being.</p> <p>4) This is my first time asking a question on StackOverflow.com, so if I did something wrong, then I'm still learning.</p> <p>5) I'm using Visual C++ 2008 Express Edition.</p> <p>Again, my query: why does the program come up with a run-time error when deleting the only node in the queue?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload