Queue implementation - program ends with an error

So my Queue implementation does basically what it's suppose to do, but ends with
_BLOCK_TYPE_IS_VALID(pHead->nBlockUse) error.
Can someone help me out understand what's going on?
Here's the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include <iostream>
#include <string>
using namespace std;

class Node{
	private:
		Node* next;
		string stringNode;
	public:
		Node(string s){
			stringNode = s;
			next = nullptr;
		}
		friend class LinkedQueue;
};

class LinkedQueue{
private:
	Node* first;
	Node* last;
public:
	LinkedQueue();
	~LinkedQueue();
	LinkedQueue& enqueue(string s);
	LinkedQueue& dequeue();
	string pop();
	void displayQueue();
	friend class Node;
};

LinkedQueue::LinkedQueue(){
	first = nullptr;
	last = nullptr;
}


/**
LinkedQueue destructor
*/
LinkedQueue::~LinkedQueue(){
	Node* toDelete = first;
	while(toDelete != nullptr){
		first = first->next;
		delete toDelete;
		toDelete = first;
	}
	delete first;
	last = nullptr;
	delete toDelete;
}

/***
adds a new element in queue
*/
LinkedQueue& LinkedQueue::enqueue(string s){
	Node *element = new Node(s);
	if(first == nullptr)
		first = last = element;
		
	/*else if(first == last){ no need actually because last->next modifies first too ...
		last = element;
		first->next = element;
	}*/ 
	else{
		last->next = element;
		last = element;
	}
	return *this;
}

LinkedQueue& LinkedQueue::dequeue(){
	if(first == nullptr){
		cout << "NOTHING TO DEQUEUE" << endl;
		return *this;
	}
	Node* x = first;
	first = first->next;
	cout << "Dequeue string: " << x->stringNode << endl;
	delete x;
	return *this;

}

void LinkedQueue::displayQueue(){
	cout << "Printing Queue..." << endl;
	if(first == nullptr){
		cout << "Nothing to print!" << endl;
		return;
	}
	Node* current = first;
	while(current != nullptr){
		cout << current->stringNode << endl;
		current = current->next;
	}
	delete current;
}

int main(){
	cout << "Main: " << endl;
	LinkedQueue queue;
	queue.enqueue("First string in the queue");
	queue.dequeue();
	queue.enqueue("Second string in the queue");
	queue.enqueue("Third string");
	queue.enqueue("String no 4");
	queue.displayQueue();
	delete &queue;
	cout << "Exit point!" << endl;
	return 0;
}


Any help is much appreciated.
1
2
3
	LinkedQueue queue;
	//...
	delete &queue;

queue was not allocated with new, so you may not use delete on it.
Nice, thanks cire, that was fast :). So I've changed my main with:
1
2
3
4
5
6
7
8
9
10
11
cout << "Main: " << endl;
	LinkedQueue *queue = new LinkedQueue();
	queue->enqueue("First string in the queue");
	queue->dequeue();
	(*queue).enqueue("Second string in the queue");
	queue->enqueue("Third string");
	queue->enqueue("String no 4");
	queue->displayQueue();
	delete queue;
	cout << "Exit point!" << endl;
	return 0;


Now I get this beauty:
'LinkedQueue.exe' (Win32): Loaded 'E:\Home\LinkedQueue\LinkedQueue\Debug\LinkedQueue.exe'. Symbols loaded.
'LinkedQueue.exe' (Win32): Loaded 'C:\Windows\SysWOW64\ntdll.dll'. Cannot find or open the PDB file.
'LinkedQueue.exe' (Win32): Loaded 'C:\Windows\SysWOW64\kernel32.dll'. Cannot find or open the PDB file.
'LinkedQueue.exe' (Win32): Loaded 'C:\Windows\SysWOW64\KernelBase.dll'. Cannot find or open the PDB file.
'LinkedQueue.exe' (Win32): Loaded 'C:\Windows\SysWOW64\msvcp110d.dll'. Symbols loaded.
'LinkedQueue.exe' (Win32): Loaded 'C:\Windows\SysWOW64\msvcr110d.dll'. Symbols loaded.
The program '[6112] LinkedQueue.exe' has exited with code 0 (0x0).

I've disabled the load messages in Options but I'm still left with "exited with code 0 (0x0)". Is this normal?
I've disabled the load messages in Options but I'm still left with "exited with code 0 (0x0)". Is this normal?


Yes:
return 0;

If you're using Visual Studio and the console doesn't stay open, ensure that, in your project's properties, you have set the link subsystem to Console.

http://s29.postimg.org/o9tps750n/subsystem.png
Last edited on
Exactly, the console doesn't stay open... yet the project has the link subsystem set to console...
You also need to select "Start without debugging" (Ctrl + F5 by default) in order for the console to stay open. If you're debugging, I suppose they think you can handle it yourself. ;)
Yeap... unfortunately that's what i'm doing... ctrl+f5. So debugger doesn't start for nothing apparently...
Topic archived. No new replies allowed.