Trying to figure out if there's a problem with my queue

Part of my assignment is to implement various container types, and my queue is giving me some weird memory errors. I won't post my entire code since it's pretty involved, but I would like to figure out if the issue is the queue itself or the rest of my program, so could someone point out the errors in my queue, if any? Thanks.

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
///////////////////////////////
/*	Exception Classes    */
///////////////////////////////

class Overflow{};
class Underflow{};

///////////////////////////////
/*	Base Class	     */
///////////////////////////////

template <typename T>
class Container
{
	public:
		virtual void add(T* obj) = 0;
		virtual T* remove() = 0;
		virtual bool isEmpty() = 0;
};

///////////////////////////////
/*	Queue	             */
///////////////////////////////
template <class T>
void add(T* obj)
{
	if(length >= size)
	{
		Overflow error;
		throw error;
	}
	
	array[tail++] = obj;
	length++;
	if(tail > size-1)
		tail -= size;
}

template <class T>
T* remove()
{
	if(isEmpty())
	{
		Underflow error;
		throw error;
	}
		
	T* x = array[head++];
	length--;
	
	if(head > size - 1)
		head -= size;
	return x;
}

template <class T>
bool isEmpty()
{
	return (length == 0);
}

template <class T>
Queue(int max):
size(max)
{
	array = new T*[size];
	length = 0;
	head = 0;
	tail = 0;
}

template <class T>
Queue<T>::~Queue()
{
	delete [] array;
}

template <class T>
void Queue<T>::copyAll(const Queue &copy)
{ 
	length = copy.length;
	head = copy.head;
	tail = copy.tail;
	array = new T*[size];
	for(int i=0; i<size; i++)
		array[i] = copy.array[i];
}

emplate <class T>
Queue<T>::Queue(const Queue &copy):
size(copy.size)
{
	copyAll(copy);
}

template <class T>
Queue<T> &Queue<T>::operator=(const Queue &q)
{
	if(this != q)
	{
		delete [] array;
		copyAll(q);
	}
	
	return *this;
}
Hello,

well I am not sure I can help but as a third person seeing your code I figure out you are trying to create an abstract class name Queue based on class Container, right?

Well for starters I didn't see where you define the Queue class.

Line 89 misses a t (not big deal here).

I didn't get what are you trying to do in lines 62-70. You are defining an abstract function named Queue as your class. I guess you are defining a constructor here but you are not telling where it belongs (like Queue:Queue). The same problem (though not constructors) for the functions above.

Finally as far as I know to make it possible for derived classes of a fully abstract class (like Container) you should define ALL your virtual function (I spoted 3 here). Otherwise you won't be able to construct objects of that class (e.g. Queue).

Maybe some of the code missing is on your another part of the code but I gave it a try.
Topic archived. No new replies allowed.