Issues defining a queue in a templated static class

Started by
9 comments, last by Darsin 15 years, 5 months ago
Hello, I'm trying to template my ThreadHandler Class as shown below. problem is that whatever i try i keep getting errors on the line where the queue is being defined. and if it manages to parse past that i get lnk errors. AI.cpp

ThreadHandler<PathStruct>::addAction(new PathStruct(pEntity, pEntity->pAI, moveToPosition, shortestPath, v, tempTerrain));

ThreadHandler.h

#pragma once

struct PathStruct;

template <typename T>
class ThreadHandler 
{
private:
	static queue<T*>						waiting_line;
	static bool								threadDead;	
public:
	static void addAction(T* action);
	static void KillThread();
	static void Run(void *);
protected:
};


ThreadHandler.cpp

#include "Stdafx.h"

template <typename T> queue<T*>		ThreadHandler<T>::waiting_line;
template <typename T> bool			ThreadHandler<T>::threadDead;

struct PathStruct
{
	Entity * entity;
	AI * ai;
	D3DXVECTOR3 moveToPos;
	stack<D3DXVECTOR3> shortPath;
	TerrainVertex * terVert;
	Terrain * terrain;
	PathStruct::PathStruct(Entity * pEntity, AI * pAI, D3DXVECTOR3 moveToPosition, stack<D3DXVECTOR3> shortestPath, 
							TerrainVertex * v, Terrain * pTerrain)
	{
		entity = pEntity;
		ai = pAI;
		moveToPos = moveToPosition;
		shortPath = shortestPath;
		terVert = v;
		terrain = pTerrain;
	}
};

template <typename T> void ThreadHandler<T>::addAction(T * action)
{
	waiting_line.push(action);
}

template <typename T> void ThreadHandler<T>::KillThread()
{
	while(!threadDead)
		Sleep(100);

	while(!waiting_line.empty())
	{
		waiting_line.pop();
	}
}

template <typename T> void ThreadHandler<T>::Run(void * pointless)
{
	threadDead = false;
	while(Main::applicationRunning)
	{
		while(!waiting_line.empty() && Main::applicationRunning) 
		{
			AI::CalcPath(waiting_line.front());
			waiting_line.pop();
		}
		Sleep(100);
	}
	threadDead = true;
}


What am i doing wrong.
Advertisement
Unless your compiler supports the export keyword, and I'm willing to bet that yours doesn't, then you can't put the definition of a template in a separate source file without explicit instantiation for specific types. Without explicit instantiation, the complete definition of the template needs to be available at point of instantiation, which means, in effect, that the definition needs to go into the header. (Or an inline file of some sort, etc.)
Could you provide me with a small sample that would apply to my code?
since i think i understand what you mean but have no idea of how to solve the issue at hand.
As with all template classes, their definition and implementation have to be defined within the header file. You've declared them but not implemented them (I see you have but in the cpp), so there will be link errors when you compile your code.

You can implement them in 2 ways, within the class definition itself, if its really simple I suggest you do that, or you can implement them outside the class definition scope, either way they have to be in the header or included within the header in another file ( sometimes people use .inl files to implement template code too keep the header clean )

Other than that I don't see anything wrong.

Good Luck!

-ddn

So what exactly do i need to do.
I've worked with templates well in the past but it's been so long ago i can't remember alot of it.

What part of put the definitions in your header file are you have trouble with?
The one that is giving me headaches is the queue.
Copy the line from your source file. Paste it into the header file somewhere after the class definition.
so like this?

[source language="cpp"]private:		static queue<T*>		ThreadHandler<T>::waiting_line;		static bool			ThreadHandler<T>::threadDead;


and what for my methods?
Quote:Original post by Darsin
so like this?

No. You're really making things a lot harder than they need to be. Open your original code. Take your mouse. Select the code in your source file. Hit Ctrl-X to move the text to the clipboard. Open your header file. Put the cursor somewhere after your class definition. Hit Ctrl-V to paste the text to the header file.

This topic is closed to new replies.

Advertisement