Template Help

Started by
5 comments, last by the_grip 22 years, 8 months ago
i'm trying to develop a template for a linked list. This is the first template i've ever worked on... Here's the header for it:

struct LISTDATA_TYP
{
public:
	LISTDATA_TYP();
	~LISTDATA_TYP();
	void *m_pData;
	int m_nNodeID;
	void* m_pNextNode;	
	void* m_pPrevNode;
};

template  class T  class LinkedListTemplate
{
public:
	LinkedListTemplate() {m_iListElementIndex = 0;}
	~LinkedListTemplate() {DestroyList();}
	int InsertElementInList(void *pDataElement);
	int DeleteElementInList(long nID);
	T* FindNodeInList(long nSearchNodeID);
	
protected:
	int AddNewNode(T* pNewNode);
	void RemoveNode(T* pDeleteNode);
	long SetNewListElementID();
	void DestroyList();

	LISTDATA_TYP  ListData;
	T *m_pHeadNode;
	T *m_pTailNode;
	long m_iListElementIndex;
};
      
And, in another file (called main.h):

LinkedListTemplate long LinkedList;
      
When i compile, i get the error:

main.obj : error LNK2001: unresolved external symbol "protected: void __thiscall LinkedListTemplate long ::DestroyList(void)" (?DestroyList@?$LinkedListTemplate@J@@IAEXXZ)
      
i couldn't get the arrow symbols surrounding class T to come through on this post, nor around long in the main.h file. But they are there. i also declare the functions in the source file like this:

template class T 
	T* LinkedListTemplate T ::FindNodeInList(long nSearchNodeID)
 
Again, the arrows around class T and T don't come through... i know what the error means, but i don't know how to fix it. i originally had the constructor and destructor in the body of the source file instead of the header, but i kept getting the above header for both, so i tried to simplify them in the header file. Any suggestions? Thanks! "You call him Dr. Grip, doll!" Edited by - the_grip on July 30, 2001 8:58:28 AM
"You call him Dr. Grip, doll!"
Advertisement
You need to explicitly instantiate the template class with long as the type...

Stick this somewhere in your code.....

template class LinkedListTemplate < long >;

Edited by - Sandman on July 30, 2001 9:14:36 AM
Ok, now my main.h header reads...

//Main.h - main header file#ifndef __ONLY__DEFINE__ONCE__#include "LinkedList.h"template class LinkedListTemplate < long >;//////////CLASSESGameInterface *m_BlastBallGameInterface;GameUtility *m_BlastBallUtility;LinkedListTemplate < long > LinkedList;//////////PROTOTYPES//Functions//int LoadBitmapResourceorFile(LPDIRECTDRAWSURFACE7 lpdds, int iWidth, int iHeight, int xDest, int yDest, int nResID, char* sFileName);int InitializeGame();int InitializeDDraw7();int RunGameTurn();int ShutdownGame();int InitializeBall(struct BlastBall *pBall, LPDIRECTDRAWSURFACE7 lpddsBallSurface, int iInitAttributes=1);////////////////GLOBAL VARIABLES///////////////////VARIABLES	HWND			main_window_handle = NULL;	HINSTANCE		main_window_hinstance = NULL;//DirectX Stuff	LPDIRECTDRAW7			lpdd7;	DDSURFACEDESC2			ddsd;	LPDIRECTDRAWSURFACE7	lpddsPrimary;	LPDIRECTDRAWSURFACE7	lpddsBackBuffer;	LPDIRECTDRAWSURFACE7	lpddsPaddle;	LPDIRECTDRAWSURFACE7	lpddsBallOffscreen;	LPDIRECTDRAWSURFACE7	lpddsBackground;	LPDIRECTDRAWSURFACE7	lpddsTemp;	LPDIRECTDRAWSURFACE7	lpddsBlocks;	LPDIRECTDRAWCLIPPER		lpddClipper;	int m_iPaddleXPosition, m_iPaddleYPosition;	int m_iXVelocityGen, m_iYVelocityGen;//Structures	struct BlastBall	{	public:		char* cBallFileName;		int iMaxVelocity;		int iMinVelocity;		int iXVelocity;		int iYVelocity;		int iAngleOfMotion;		int iXDirection;		int iYDirection;		int iXPoint, iYPoint;		int iXPosition, iYPosition;		bool bBallDeath;	} *m_pBlastBall;//////////////////////////////////////////////////#define __ONLY__DEFINE__ONCE__#endif 


But i get the following errors...
d:\devl\personal\cpp\blastball\linkedlist.h(22) : warning C4661: ''int __thiscall LinkedListTemplate < long > ::InsertElementInList(void *)'' : no suitable definition provided for explicit template instantiation request        d:\devl\personal\cpp\blastball\linkedlist.h(24) : see declaration of ''InsertElementInList''d:\devl\personal\cpp\blastball\linkedlist.h(22) : warning C4661: ''int __thiscall LinkedListTemplate < long > ::DeleteElementInList(long)'' : no suitable definition provided for explicit template instantiation request        d:\devl\personal\cpp\blastball\linkedlist.h(25) : see declaration of ''DeleteElementInList''d:\devl\personal\cpp\blastball\linkedlist.h(22) : warning C4661: ''long *__thiscall LinkedListTemplate < long > ::FindNodeInList(long)'' : no suitable definition provided for explicit template instantiation request        d:\devl\personal\cpp\blastball\linkedlist.h(26) : see declaration of ''FindNodeInList''d:\devl\personal\cpp\blastball\linkedlist.h(22) : warning C4661: ''int __thiscall LinkedListTemplate < long > ::AddNewNode(long *)'' : no suitable definition provided for explicit template instantiation request        d:\devl\personal\cpp\blastball\linkedlist.h(29) : see declaration of ''AddNewNode''d:\devl\personal\cpp\blastball\linkedlist.h(22) : warning C4661: ''void __thiscall LinkedListTemplate < long > ::RemoveNode(long *)'' : no suitable definition provided for explicit template instantiation request        d:\devl\personal\cpp\blastball\linkedlist.h(30) : see declaration of ''RemoveNode''d:\devl\personal\cpp\blastball\linkedlist.h(22) : warning C4661: ''long __thiscall LinkedListTemplate::SetNewListElementID(void)'' : no suitable definition provided for explicit template instantiation request        d:\devl\personal\cpp\blastball\linkedlist.h(31) : see declaration of ''SetNewListElementID''d:\devl\personal\cpp\blastball\linkedlist.h(22) : warning C4661: ''void __thiscall LinkedListTemplate < long > ::DestroyList(void)'' : no suitable definition provided for explicit template instantiation request        d:\devl\personal\cpp\blastball\linkedlist.h(32) : see declaration of ''DestroyList''Linking...main.obj : error LNK2001: unresolved external symbol "public: __thiscall LISTDATA_TYP::LISTDATA_TYP(void)" (??0LISTDATA_TYP@@QAE@XZ)main.obj : error LNK2001: unresolved external symbol "public: __thiscall LISTDATA_TYP::~LISTDATA_TYP(void)" (??1LISTDATA_TYP@@QAE@XZ)main.obj : error LNK2001: unresolved external symbol "protected: void __thiscall LinkedListTemplate::DestroyList(void)" (?DestroyList@?$LinkedListTemplate@J@@IAEXXZ) 


Any more help is appreciated! Thanks - i''m new to the template stuff.

"You call him Dr. Grip, doll!"
"You call him Dr. Grip, doll!"
hmmmm. Whats in the cpp for the template?

If you cant get it to work, stick all the functions in the header. Thats usually the easiest way.
Shouldn''t be all the template classes, including its actual function body, place in the include file instead of the usual .cpp/.h combo?

... just wondering...
"after many years of singularity, i'm still searching on the event horizon"
Yup, seems to work. Like i said, this is my first :D

Thanks for the help!

(p.s. the above code is broken anyway... i was writing it at 4 in the morning last night)

"You call him Dr. Grip, doll!"
"You call him Dr. Grip, doll!"
quote:
Shouldn''t be all the template classes, including its actual function body, place in the include file instead of the usual .cpp/.h combo?

... just wondering...


I dont think you have to, but it does make life easier...

This topic is closed to new replies.

Advertisement