Templated lists, could you check this

Started by
3 comments, last by JinJo 21 years ago
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> *head;
	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()
{
	head = NULL;
	current = NULL;
	numberOfNodes = 0;
}

template <class T> void E_SingleList<T>::StartIterator()
{
	current = head;
}

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?
Advertisement
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.
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
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.
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
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.

This topic is closed to new replies.

Advertisement