#### Archived

This topic is now archived and is closed to further replies.

# Templated lists, could you check this

This topic is 5378 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Thought i would do lists using templates and here is my header, note this is for a singly linked list and theres no comments in it yet and may be a little messy

//E_Lists.h

#ifndef E_LISTS
#define E_LISTS

template <class T> class E_Node
{
protected:
T data;
E_Node *next;
};

template <class T> class E_SingleList
{
protected:
E_Node<T> *current;
int numberOfNodes;

public:
//constructor(s) / destructor

void Create();
void StartIterator();
void MoveForward();
T GetCurrentItem();
bool IsIteratorValid();
void InsertItem(T item);
void RemoveCurrentItem();
void AppendItem(T item);
int GetNumberOfNodes();
};

#endif

heres some source of the implementation

//E_Lists.cpp

#include "E_Lists.h"

template <class T> void E_SingleList<T>::Create()
{
current = NULL;
numberOfNodes = 0;
}

template <class T> void E_SingleList<T>::StartIterator()
{
}

template <class T> void E_SingleList<T>::MoveForward()
{
if(IsIteratorValid())
{
current = current->next;
}
}

template <class T> T E_SingleList<T>::GetCurrentItem()
{
return current->data;
}

dont think the rest is relevent. Right now I declare a list like this: E_SingleList list; then in my program i use it like this list.Create(); and it comes up with a compiler error saying E_App.obj : error LNK2001: unresolved external symbol "public: void __thiscall E_SingleList::Create(void)" (?Create@?\$E_SingleList@H@@QAEXXZ) D anyone know whats wrong?

##### Share on other sites
Poor support for templates, in a nutshell. It''s a complicated issue. One workaround is to put the function definitions inside the class definition, not just the function headers/declarations.

##### Share on other sites
Thanks for the fast reply. I tried what u said and i think it works, could you (or anyone else) explain why this is, im using vc++6 and is there any other way to fix this because it looks realy messy now

##### Share on other sites
AFAIK according to the standard you can use the keyword extern to make it work, however I can''t think of any compilers that let you do this, so you are probably stuck with what Dobbs suggested.

##### Share on other sites
quote:
Original post by JinJo
Thanks for the fast reply. I tried what u said and i think it works, could you (or anyone else) explain why this is
Templates need to produce different code for different template parameters. If the templated code is compiled in a cpp, how could the compiler use this compiled code to produce new instantiations of the template? All the type information etc. would be lost when the code was compiled. It''s as simple as that.

• ### Forum Statistics

• Total Topics
628718
• Total Posts
2984375

• 25
• 11
• 10
• 14
• 14