Confused about Implementing a Template

Hello all, I am trying to implement Template to my code listed below but I guess that I am very confused on the proper way to go about doing this. I read a couple of tuturials and it just isn't clicking I am still getting a few errors in my code. Other than the Attempted Class implementation the code works and runs properly. Can someone please point me in the correct direction and explain to me what I am doing wrong? Thanks in advance!

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
//Header
#include <iostream>
using namespace std;
#define MAX_QUEUE 15

template <class T> // Create Template
class ADTQueue // class
{
private:
	int q[MAX_QUEUE], front, rear;
public:
	ADTQueue()
	{
		front = -1;
		rear = -1;
	}

	// Function for Enqueue
	template <class T>
	void Enqueue<T>(T element)
	{
		if (isEmpty())
		{
			q[++rear] = element;
			front = rear;
			return;
		}
		else if (isFull())
		{
			cout << "Queue is full. No more elements can be inserted.";
			return;
		}
		rear = (rear + 1) % MAX_QUEUE;
		q[rear] = element;
	}

	// Function for Dequeue
	template <class T>
	void Dequeue(T)
	{
		if (isEmpty())
		{
			cout << endl << "Queue is empty.No more elements cqn be deleted.";
			return;
		}
		else if (front == rear)
		{
			cout << endl << "Deleted element is : " << q[front];
			front = rear = -1;
			return;
		}
		cout << endl << "Deleted element is : " << q[front];
		front = (front + 1) % MAX_QUEUE;
	}

	bool isEmpty() // Check to see if Queue is emtpy and return
	{
		if (front == -1 && rear == -1)
			return true;
		else
			return false;
	}
	bool isFull() // Check to see if Queue is full and return
	{
		if (front == (rear + 1) % MAX_QUEUE)
			return true;
		else
			return false;
	}
	// Function for PrintQueue
	template <class T>
	void PrintQueue(T)
	{
		cout << endl << "Queue is : ";
		int i;
		if (isEmpty())
			return;
		if (front <= rear)
		{
			for (i = front; i <= rear; i++)
				cout << endl << q[i] << " ";
		}
		else
		{
			for (i = front; i <= MAX_QUEUE - 1; i++)
			{
				cout << endl << q[i] << " ";
			}
			for (i = 0; i <= rear; i++)
			{
				cout << endl << q[i] << " ";
			}
		}
		cout << endl;
	}
};
 


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
//Main
#include <iostream>
#include "ADTqueue.h"
using namespace std;
#define MAX_QUEUE 15

int main()
{
	ADTQueue queue;
	int ch;
	while (1)
	{
		cout << "\n1.Enquque   2.Dequeue   3.Print Queue    4.EXIT\n\nEnter ur choice : ";
		cin >> ch;
		switch (ch)
		{
		case 1: cout << "Enter element : ";
			cin >> ch;
			queue.Enqueue(ch); break;
		case 2: queue.Dequeue(); break;
		case 3: queue.PrintQueue(); break;
		case 4: exit(0);
		}
	}
	cout << endl;
	system("pause");
	return 0;
}
 
When you define the functions inside the class definitions you should not repeat the template parameter list.

1
2
3
4
5
6
7
8
9
10
11
12
template <class T>
class ADTQueue
{
	...
	template <class T>
	void Enqueue<T>(T element)
	{
		...
	}
	
	...
};



If you decide to write the function definitions outside the class template definition, then you need to repeat template <class T>.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
template <class T>
class ADTQueue
{
	...
	
	void Enqueue(T element);
	
	...
};

template <class T>
void ADTQueue<T>::Enqueue(T element)
{
	...
}
Last edited on
Still getting multiple errors am I missing something? I chose to define functions inside the class definitions. So I removed the repeat template parameter lists.

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
#include <iostream>
using namespace std;
#define MAX_QUEUE 15

template <class T> // Create Template
class ADTQueue // class
{
private:
	int q[MAX_QUEUE], front, rear;
public:
	ADTQueue()
	{
		front = -1;
		rear = -1;
	}

	// Function for Enqueue
	void Enqueue (T element)
	{
		if (isEmpty())
		{
			q[++rear] = element;
			front = rear;
			return;
		}
		else if (isFull())
		{
			cout << "Queue is full. No more elements can be inserted.";
			return;
		}
		rear = (rear + 1) % MAX_QUEUE;
		q[rear] = element;
	}

	// Function for Dequeue
	void Dequeue(T)
	{
		if (isEmpty())
		{
			cout << endl << "Queue is empty.No more elements cqn be deleted.";
			return;
		}
		else if (front == rear)
		{
			cout << endl << "Deleted element is : " << q[front];
			front = rear = -1;
			return;
		}
		cout << endl << "Deleted element is : " << q[front];
		front = (front + 1) % MAX_QUEUE;
	}

	bool isEmpty() // Check to see if Queue is emtpy and return
	{
		if (front == -1 && rear == -1)
			return true;
		else
			return false;
	}
	bool isFull() // Check to see if Queue is full and return
	{
		if (front == (rear + 1) % MAX_QUEUE)
			return true;
		else
			return false;
	}
	// Function for PrintQueue
	void PrintQueue(T)
	{
		cout << endl << "Queue is : ";
		int i;
		if (isEmpty()) 
			return;
		if (front <= rear)
		{
			for (i = front; i <= rear; i++)
				cout << endl << q[i] << " ";
		}
		else
		{
			for (i = front; i <= MAX_QUEUE - 1; i++)
			{
				cout << endl << q[i] << " ";
			}
			for (i = 0; i <= rear; i++)
			{
				cout << endl << q[i] << " ";
			}
		}
		cout << endl;
	}
};
You have not specified the template argument when creating the queue.
1
2
// queue of integers
ADTQueue<int> queue;


If you don't want Dequeue to take any arguments you should declare it without parameters (same with PrintQueue).
1
2
void Dequeue(T)
void Dequeue()
Awesome! I get it now thanks Peter87
Topic archived. No new replies allowed.