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
#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.
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)
#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.