Runtime error: Unhandled exception at

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
#include <iostream>
#include "Queue.h"
#include "Customer.h"

using namespace std;

int main() {
	Queue q1;

	for (int i = 0; i < 13; i++)
	{
		Customer c(i);
		bool in = q1.enqueue(c);  // Here is the problem!
		if (i <= 9 && !in)
		{
			cout << "ERROR: default size is smaller than 10!!" << endl;
		}
		else if (i > 9 && in)
		{
			cout << "ERROR: default size is bigger than 10!!" << endl;
		}
	}
	q1.print();
	getchar();
	return 0;
}


Queue.cpp
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
#include <iostream>
#include "Queue.h"
#include "Customer.h"

Queue::Queue(){
	if ( (_contents = new Customer) == 0 ) {
		cout << "Memmory allocation Failed." << endl;
		_size = 0;
	} else {
		_size = 10;
	}
	_front = -1;
	_rear = -1;
}

Queue::Queue(int size){
	if ( (_contents = new Customer[size]) == 0 ) {
		cout << "Memmory allocation Failed." << endl;
		_size = 0;
	} else {
		_size = size;
	}
	_front = -1;
	_rear = -1;
}
Queue::~Queue() {
	if (_size != 0) 
		delete []_contents;
}
bool Queue::enqueue(Customer c){
	if((_front==0 && _rear==_size) || (_front==(_rear+1))){
		cout << "Queue is Full" << endl;
		return false;
	} else {
		if(_front==(-1) && _rear==(-1)){
			_rear=0;
			_front=0;
		} else {
			if(_front!=0 && _rear==_size){
				_rear=0;
			} else {
				_rear=_rear+1;
			}
		_contents[_rear]=c;
		return true;
		}
	}
	return false;
}


void Queue::print(){
	int index = _front;    
    int queuelength = (_rear+_size-_front) % _size;
    for(int index = _front; index <= _front + queuelength; index++)
    {
		cout << _contents[(index+_size) % _size].getId() << " ";
    }
	cout << endl;
}


Customer.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifndef _CUSTOMER_H_
#define _CUSTOMER_H_

#include <string>
using namespace std;

class Customer {
public:
	Customer(int id=1111, string name="lili"):_id(id),_name(name){}

	int getId() const {return _id;}
	string getName() const {return _name;}

private:
	int _id;
	string _name;
};

#endif 
Last edited on
What does your Queue.h file look like?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#ifndef _QUEUE_H_
#define _QUEUE_H_
#include "Customer.h"
using namespace std;

class Queue {
public:

	Queue();
	Queue(int size);
	~Queue();
	bool enqueue(Customer c);
	Customer dequeue();
	void print();

private:
	Customer* _contents;
	int _size;
	int _front;
	int _rear;
};

#endif 
closed account (DSLq5Di1)
1
2
int main() {
	Queue q1;

1
2
3
4
5
6
7
8
9
10
Queue::Queue(){
	if ( (_contents = new Customer) == 0 ) {
		cout << "Memmory allocation Failed." << endl;
		_size = 0;
	} else {
		_size = 10;
	}
	_front = -1;
	_rear = -1;
}

1
2
...
		bool in = q1.enqueue(c);  // Here is the problem! 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
bool Queue::enqueue(Customer c){
	if((_front==0 && _rear==_size) || (_front==(_rear+1))){
		cout << "Queue is Full" << endl;
		return false;
	} else {
		if(_front==(-1) && _rear==(-1)){
			_rear=0;
			_front=0;
		} else {
			if(_front!=0 && _rear==_size){
				_rear=0;
			} else {
				_rear=_rear+1;
			}
		_contents[_rear]=c;
		return true;
		}
	}
	return false;
}

1
2
3
4
Queue::~Queue() {
	if (_size != 0) 
		delete []_contents;
}
Sorry, but can you please explain to me what is the problem with _contents ?
closed account (DSLq5Di1)
Your default constructor in Queue is only creating one Customer object, but you are using _contents as though it is an array of 10 Customers.
i fixed it with:
1
2
 Queue::Queue(){
	if ( (_contents = new Customer[10]) == 0 ) {

same problem :(
Ok thanks man, it`s really the problem.
i Found a problem somewhere else :)
thank you so much
You always have to initialize arrays before accessing them.
Last edited on
Topic archived. No new replies allowed.