template ,h and .cpp full of errors

So in my nativity and rush into templates I made the mistake of splitting the template class that I created into an h file and cpp file. After reading http://www.codeproject.com/Articles/48575/How-to-define-a-template-class-in-a-h-file-and-imp I figured it out and added an include to the source file at the bottom of the h file, Now I have a ton of errors that I have no idea how to fix

the following code blocks a quite huge, for which I apologize

first my .h file

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
#ifndef _EMBEDLINKEDLIST_
#define _EMBEDLINKEDLIST_

//#include "Iemblist.h"

template<class T>
class EmbedLinkedList
{
private:
	T * first;
	T * last;

	T * itCurrent;

	int mySize;

public:

	EmbedLinkedList<T>();

	int size();

	void add(T & data);
	void remove(int i, bool shouldDelete);
	void remove(T * item, bool souldDelete);

	T& get(int index);

	void resetItCurrent();
	void setItCurrent(int index);
	void setItCurrent(T * data);
	void itCurrentNext();
	void itCurrentPrev();

	T& getItCurrent();

	void removeViaIt(int index, bool forwardOrBackward);

	T & operator [] (int index);

};
#include "EmbedLinkedList.cpp"

#endif 


now my .cpp file

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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145

#include "EmbedLinkedList.h"
// you may think that this is what is causing it, but I've changed it back and forth to no avail 

template<class T>
EmbedLinkedList<T> :: EmbedLinkedList()
{
	size = 0;
}

template<class T>
int EmbedLinkedList<T> :: size()
{
	return mySize;
}

template<class T>
void EmbedLinkedList<T> :: add(T & data)
{
	if (mySize == 0)
	{
		first = &data;
		last = &data;
	}
	else
	{
		T & current = *last;
		last = &data

		current.setNext(data);
		data.setPrev(current);
	}
	size++;
}

template <class T>
void EmbedLinkedList<T> :: remove (int index, bool shouldDelete = false)
{
	T * current, prev = nullptr, next = nullptr;

	current = &get(index);

	if (current -> doesHaveNext())
	{
		next = &(current -> getNext());
	}
	if (current -> doesHavePrev())
	{
		prev = &(current -> getPrev());
	}

	next -> setPrev(prev);
	prev -> setNext(next);

	if (shouldDelete)
	{
		delete current;
	}

	size--;
}

template <class T>
void EmbedLinkedList<T> :: remove (T * data, bool shouldDelete = false)
{
	T * prev = nullptr, next = nullptr;
	if (data -> doesHaveNext())
	{
		next = &(data -> getNext());
	}
	if (data -> doesHavePrev())
	{
		prev = &(data -> getPrev());
	}

	next -> setPrev(prev);
	prev -> setNext(next);

	if (shouldDelete)
	{
		delete data;
	}
	size--;
}

template <class T>
T& EmbedLinkedList<T> :: get(int index)
{
	bool fromStart = (index < mySize/2);

	T * current;
	if (fromStart)
	{
		current = *first;
		for (int i = 0; i < index; i++)
		{
			current = &(current.getNext());
		}
	}
	else
	{
		current = *last;
		for (int i = (mySize -1); i > index; i--)
		{
			current = &(current.getPrev());
		}
	}
	return *current;
}

template <class T>
void EmbedLinkedList<T> :: resetItCurrent()
{
	itCurrent = first;
}

template <class T>
void EmbedLinkedList<T> :: setItCurrent(int index)
{
	itCurrent = &get(index);
}

template <class T>
void EmbedLinkedList<T> :: setItCurrent(T * data)
{
	itCurrent = data;
}

template <class T>
void EmbedLinkedList<T> :: itCurrentNext()
{
	itCurrent = &(itCurrent -> getNext());
}

template <class T>
void EmbedLinkedList<T> :: itCurrentPrev()
{
	itCurrent = &(itCurrent -> getPrev());
}

template <class T>
T & EmbedLinkedList<T> :: getItCurrent()
{
	return *itCurrent;
}


and finally the errors,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Error	1	error C2659: '=' : function as left operand	c:\documents and settings\q.dot\my documents\code librarys\of_v0.7.4_vs2010_release\apps\myapps\flgame\embedlinkedlist.cpp	8
Error	2	error C2995: 'EmbedLinkedList<T>::EmbedLinkedList(void)' : function template has already been defined	c:\documents and settings\q.dot\my documents\code librarys\of_v0.7.4_vs2010_release\apps\myapps\flgame\embedlinkedlist.cpp	9
Error	3	error C2995: 'int EmbedLinkedList<T>::size(void)' : function template has already been defined	c:\documents and settings\q.dot\my documents\code librarys\of_v0.7.4_vs2010_release\apps\myapps\flgame\embedlinkedlist.cpp	15
Error	4	error C2995: 'void EmbedLinkedList<T>::add(T &)' : function template has already been defined	c:\documents and settings\q.dot\my documents\code librarys\of_v0.7.4_vs2010_release\apps\myapps\flgame\embedlinkedlist.cpp	34
Error	5	error C2572: 'EmbedLinkedList<T>::remove' : redefinition of default parameter : parameter 2	c:\documents and settings\q.dot\my documents\code librarys\of_v0.7.4_vs2010_release\apps\myapps\flgame\embedlinkedlist.cpp	61
Error	6	error C2995: 'void EmbedLinkedList<T>::remove(int,bool)' : function template has already been defined	c:\documents and settings\q.dot\my documents\code librarys\of_v0.7.4_vs2010_release\apps\myapps\flgame\embedlinkedlist.cpp	61
Error	7	error C2572: 'EmbedLinkedList<T>::remove' : redefinition of default parameter : parameter 2	c:\documents and settings\q.dot\my documents\code librarys\of_v0.7.4_vs2010_release\apps\myapps\flgame\embedlinkedlist.cpp	84
Error	8	error C2995: 'void EmbedLinkedList<T>::remove(T *,bool)' : function template has already been defined	c:\documents and settings\q.dot\my documents\code librarys\of_v0.7.4_vs2010_release\apps\myapps\flgame\embedlinkedlist.cpp	84
Error	9	error C2995: 'T &EmbedLinkedList<T>::get(int)' : function template has already been defined	c:\documents and settings\q.dot\my documents\code librarys\of_v0.7.4_vs2010_release\apps\myapps\flgame\embedlinkedlist.cpp	109
Error	10	error C2995: 'void EmbedLinkedList<T>::resetItCurrent(void)' : function template has already been defined	c:\documents and settings\q.dot\my documents\code librarys\of_v0.7.4_vs2010_release\apps\myapps\flgame\embedlinkedlist.cpp	115
Error	11	error C2995: 'void EmbedLinkedList<T>::setItCurrent(int)' : function template has already been defined	c:\documents and settings\q.dot\my documents\code librarys\of_v0.7.4_vs2010_release\apps\myapps\flgame\embedlinkedlist.cpp	121
Error	12	error C2995: 'void EmbedLinkedList<T>::setItCurrent(T *)' : function template has already been defined	c:\documents and settings\q.dot\my documents\code librarys\of_v0.7.4_vs2010_release\apps\myapps\flgame\embedlinkedlist.cpp	127
Error	13	error C2995: 'void EmbedLinkedList<T>::itCurrentNext(void)' : function template has already been defined	c:\documents and settings\q.dot\my documents\code librarys\of_v0.7.4_vs2010_release\apps\myapps\flgame\embedlinkedlist.cpp	133
Error	14	error C2995: 'void EmbedLinkedList<T>::itCurrentPrev(void)' : function template has already been defined	c:\documents and settings\q.dot\my documents\code librarys\of_v0.7.4_vs2010_release\apps\myapps\flgame\embedlinkedlist.cpp	139
Error	15	error C2995: 'T &EmbedLinkedList<T>::getItCurrent(void)' : function template has already been defined	c:\documents and settings\q.dot\my documents\code librarys\of_v0.7.4_vs2010_release\apps\myapps\flgame\embedlinkedlist.cpp	145


I'm using visual studio c++

of course now I'm completely stuck and have now idea how to fix this
an then i figured it out as soon as I posted like i always do, soz guys. I sill want to know what your thoughts are because my soolution is potentially hackish, and yes i know on line 8 of the cpp is one of the mistakes.
Last edited on
I don't know exactly what you want to know, but don't put the template 'implementation' in a file with an extension (*.cpp) that the compiler knows and wants to translate. I'd recommend using an extension like .tpp with a t for template (or so)
You're doing all sorts of weird and non-standard things to try and get around a very straightforward issue.

When writing a template class, you should simply include all the method definitions inside your class definition, like so:

EmbedLinkedList.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
#ifndef _EMBEDLINKEDLIST_
#define _EMBEDLINKEDLIST_

//#include "Iemblist.h"

template<class T>
class EmbedLinkedList
{
private:
	T * first;
	T * last;

	T * itCurrent;

	int mySize;

public:

	EmbedLinkedList<T>()
	{
		size = 0;
	}

	int size()
	{
		return mySize;
	}

	// etc

};

#endif  


No need then to do anything weird with include statements.

Oh, and tab characters are the work of the devil. I've used them for consistency with the code of yours I was quoting, but now I feel dirty. Tell your IDE/editor to put in a fixed number of spaces instead.
Last edited on
Topic archived. No new replies allowed.