Template Issues

Hello everyone. Well, basically, what I've been doing was creating a class that would implement the concept of Double Linked List, but that would behave like a queue ( the STL implementation is deque, or Double Ended Queue ).

The problem occured when I have been trying to generalize the class using templates. I mean, why use only integers ? Why not double or char or what not ?

So, here are parts of the code, hope someone will be able to help me understand what I am doing wrong.

PS: Worked perfectly before including all the template stuff..

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
// Source.cpp <=> Main.cpp

#include <iostream>
#include "DList.h"

using namespace std;

int main(void) {
	DList<int> *list;
	list = new DList<int>();

	list->push_back(10);
	list->push_front(15);
	list->push_back(25);
	list->push_front(5);
	list->print();

	list->pop_back();
	list->pop_front();
	list->print();

	list->push_back(100);
	list->push_front(-100);
	list->print();
	
	list->get_front();
	list->get_back();

	delete list;

	system("pause");
	return 0;
}


1
2
3
4
5
6
7
8
9
10
11
12
// Node.H

#pragma once

template <class T>
class Node {

	public:
		T value;
		Node *next;
		Node *prev;
};


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
// DList.H

#include "Node.h"

#pragma once

template <class T>
class DList : public Node {

	private:
		Node *front;
		Node *back;

	public:
		DList<T>(void);
		bool empty(void);
		void push_front(T);
		void push_back(T);
		void pop_front(void);
		void pop_back(void);
		void get_front(void);
		void get_back(void);
		void print(void);
		~DList<T>(void);
};


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
// DList.cpp

#include <iostream>
#include "Node.h"
#include "DList.h"

using namespace std;

template <class T>
DList<T>::DList(void) {
	this->front = NULL;
	this->back  = NULL;
}

template <class T>
bool DList<T>::empty(void) {
	if (this->front == NULL && this->back == NULL)
		return true;
	return false;
}

template <class T>
void DList<T>::push_front(T value) {
	Node *newNode;
	newNode = new Node();
	newNode->value    = value;

	if (!(this->empty())){
		newNode->next     = this->front;
		newNode->prev     = NULL;
		this->front->prev = newNode;
		this->front       = newNode;
	}
	else{
		newNode->next  = NULL;
		newNode->prev  = NULL;
		this->front    = newNode;
		this->back     = newNode;
	}
}

template <class T>
void DList<T>::push_back(T value) {
	Node *newNode;
	newNode = new Node();
	newNode->value   = value;

	if (!(this->empty())){
		newNode->next    = NULL;
		newNode->prev    = this->back;
		this->back->next = newNode;
		this->back       = newNode;
	}
	else{
		newNode->next  = NULL;
		newNode->prev  = NULL;
		this->front    = newNode;
		this->back     = newNode;
	}
}

template <class T>
void DList<T>::pop_front(void) {
	if (!(this->empty())){
		this->front->next->prev = NULL;
		this->front             = this->front->next;
	}
}

template <class T>
void DList<T>::pop_back(void) {
	if (!(this->empty())){
		this->back->prev->next = NULL;
		this->back             = this->back->prev;
	}
}

template <class T>
void DList<T>::get_front(void) {
	cout << "Front value: " << (this->front->value) << "\n";
}

template <class T>
void DList<T>::get_back(void) {
	 cout << "Back value: " << (this->back->value) << "\n";
}

template <class T>
void DList<T>::print(void) {
	Node *node;
	node = this->front;
	cout << "Double Linked List: ";
	do{
		cout << node->value << ", ";
		node = node->next;
	}while (node != NULL);
	cout << "\n";
}

template <class T>
DList<T>::~DList(void) {
	this->front = NULL;
	this->back  = NULL;
}


The errors returned by the compiler:

Error	1	error C2955: 'Node' : use of class template requires template argument list	c:\users\jumper\desktop\c++ other\double linked list\double linked list\dlist.h	6
Error	2	error C2955: 'Node' : use of class template requires template argument list	c:\users\jumper\desktop\c++ other\double linked list\double linked list\dlist.h	6


Any help is really appreciated. Thanks in advance.
Last edited on
You must specify template arguments. For example instead of

1
2
3
4
5
6
template <class T>
class DList : public Node {

	private:
		Node *front;
		Node *back;


you should write

1
2
3
4
5
6
template <class T>
class DList : public Node {

	private:
		Node<T> *front;
		Node<T> *back;
Last edited on
I still get the exact same error :/

PS: I have tried doing that before (I think I forgot to mention)

EDIT: The compiler says that the error is on this line (actually I think it could be lower in that class too) :

class DList : public Node {

I also tried adding the <T> to that : public Node too.
I really don't know what is causing the error.
Last edited on
Template classes should have their implementation in the same file with the declaration. Also, the STLs implementation of doubly linked list is std::list.
Uhm, alright, I will check that later on. And thanks for the help :) Both of you :)

And regarding the Double Linked List.. I said I want it to behave as a queue <=> No other operations may be performed on the list apart from Push_Front, Push_Back, Pop_Front, Pop_Back, getting the front/back and printing the list.

The std::list has more operations allowed:

template < class T, class Alloc = allocator<T> > class list;
List
Lists are sequence containers that allow constant time insert and erase operations anywhere within the sequence, and iteration in both directions.
Last edited on
1
2
	DList<int> *list;
	list = new DList<int>();
sigh
DList<int> list;
So, what I meant is really std::deque, not std::list.

template < class T, class Alloc = allocator<T> > class deque;
Double ended queue
deque (usually pronounced like "deck") is an irregular acronym of double-ended queue. Double-ended queues are sequence containers with dynamic sizes that can be expanded or contracted on both ends (either its front or its back).


But thanks again, the topic is closed.


EDIT: @ne555 I tried to remove the pointer declaration and use the normal declaration. Same issue. But I will try again. Maybe it's going to work now with what @vlad from moscow also mentioned.

Thanks guys :)
Last edited on
Topic archived. No new replies allowed.