Queue Class Template

May 17, 2012 at 1:38am
Hi

I am having a little trouble implementing a queue class template, this code works fine when it isn't a template class but now gives me LNK2019 errors (indicating it can't find the function main is calling)

LNK1120: 2 unresolved externals		line 0	C/C++ Problem
LNK2019: unresolved external symbol "public: __thiscall Queue<int>::~Queue<int>(void)" (??1?$Queue@H@@QAE@XZ) referenced in function _main
LNK2019: unresolved external symbol "public: __thiscall Queue<int>::Queue<int>(int)" (??0?$Queue@H@@QAE@H@Z) referenced in function _main



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
//queue.h
#ifndef QUEUE_H_
#define QUEUE_H_


template <typename T>
class Queue
{
private:
	struct Node { T item; struct Node * next;};
	const static int Q_SIZE = 10;
	Node * front;												// pointer to front of Queue
	Node * rear;												// pointer to rear of Queue
	int count;													// current number of items in Queue
	const int qsize;											// maximum number of items in Queue

	// preemptive definitions to prevent public copying
	Queue(const Queue & q) : qsize(0) {};
	Queue & operator=(const Queue & q) {return *this;};

public:
	Queue(int qs = Q_SIZE);										// create queue with a qs limit
	~Queue();
	bool isempty() const;
	bool isfull() const;
	int queuecount() const;
	bool enqueue(const T &);									// add item to end
	bool dequeue(T &);											// remove item from front
};

#endif /* QUEUE_H_ */



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
//queue.cpp
#include "queue.h"

template <typename T>
Queue<T>::Queue(int qs)
{
	qsize = qs;
	count = 0;
	front = rear = nullptr;
}

template <typename T>
Queue<T>::~Queue()
{
	Node * temp;						// create temporary address store
	while(front != (void *) 0)			// while the queue is not empty
	{
		temp = front;
		front = front->next;			// advance the front object to the next
		delete temp;					// delete the temporary data
	}
}

template <typename T>
bool Queue<T>::isempty() const
{
	return count == 0;
}

template <typename T>
bool Queue<T>::isfull() const
{
	return count == qsize;
}

template <typename T>
int Queue<T>::queuecount() const
{
	return count;
}

template <typename T>
bool Queue<T>::enqueue(const T &data)  	// add item to end
{
	if(isfull())						// if queue is full halt queuing
		return false;

	Node * add = new Node;				// create node
	add->item = data;					// set node pointers
	add->next = (void *) 0;				// or nullptr;
	count++;
	if (front == (void *) 0)			// if queue is empty,
		front = add;					// place item at front
	else
		rear->next = add;				// else place at rear
	rear = add;							// have rear point to new node

	return true;
}

template <typename T>
bool Queue<T>::dequeue(T &data)			// remove item from front
{
	if(front == (void *) 0)				// front node is empty, queue is empty
		return false;

	data = front->item;					// set data to first item in queue
	count--;							// decrement item count
	Node * temp = front; 				// save location of first item
	front = front->next; 				// reset front to next item
	delete temp;						// delete former first item

	if (count == 0)						// if the queue is now empty set rear to point to nothing
		rear = (void *)0;

	return true;

}


1
2
3
4
5
6
7
8
9
10
11
//main.cpp
#include "queue.h"
const int SIZE = 5;

int main()
{
	Queue<int> tempint(SIZE);

	return 0;
}


Really appreciate the help/feedback - thanks!
/Saad
May 17, 2012 at 1:45am
The function implementations should also go in the header file.
May 17, 2012 at 12:11pm
anything with a template becomes an inline object. This means that it needs to be in a header.
May 18, 2012 at 4:31am
Thanks that solved the problem!


/Saad
Topic archived. No new replies allowed.