Sign in to follow this  
jujumbura

C++ , static template instantiation?

Recommended Posts

jujumbura    209
Hello all, I have a question about template static instantiation in C++. I am trying to make a Singleton template to use for some of my managers, and an example looks like this: Singleton.h
#ifndef SINGLETON_H
#define SINGLETON_H

template <class T>
class Singleton
{
public:

	Singleton();
	~Singleton();
	void Allocate();
	void Release();
	T* Get();

protected:

	T* m_Instance;
};

#endif // SINGLETON_H
Singleton.cpp
#include "stdafx.h"
#include "Singleton.h"

template<class T>
Singleton<T>::Singleton() : m_Instance( NULL )
{
}

template<class T>
Singleton<T>::~Singleton()
{
	Release();
}

template<class T>
void Singleton<T>::Allocate()
{
	if ( m_Instance == NULL )
	{
		m_Instance = new T;
	}
}

template<class T>
void Singleton<T>::Release()
{
	if ( m_Instance != NULL )
	{
		delete m_Instance;
	}
}

template<class T>
T* Singleton<T>::Get()
{
	return m_Instance;
}
ActorManager.h
#ifndef ACTORMANAGER_H
#define ACTORMANAGER_H

#include "Singleton.h"

class ActorManager
{
public :
	static Singleton<ActorManager> Instance;

private:
	ActorManager();
	~ActorManager();
};

#endif//ACTORMANAGER_H
ActorManager.cpp
#include "stdafx.h"
#include "ActorManager.h"

Singleton<ActorManager> ActorManager::Instance;

ActorManager::ActorManager()
{
}

ActorManager::~ActorManager()
{
}
But I get the following error in VS 2003 when I compile: 3DEngine error LNK2019: unresolved external symbol "public: __thiscall Singleton<class ActorManager>::Singleton<class ActorManager>(void)" (??0?$Singleton@VActorManager@@@@QAE@XZ) referenced in function _$E1 It looks like it thinks the constructor for type ActorManager is undefined, but I'm puzzled as to why. I assume this is from my instantiation of the static member "Instance" in Actormanager.cpp. I've included the template header, am I missing something? Thanks very much, jujumbura

Share this post


Link to post
Share on other sites
You can not put definition of the template in a cpp file. Each file (or translation unit) that includes your Singleton.h file, must have FULL definition of the template, before it's used.

So, move all code from Singleton.cpp into Singleton.h and it will work.

Share this post


Link to post
Share on other sites
Endar    668
Also, even though it's probably not what you want to hear, you should really think about whether the object requires only 1 instance for the whole program.

I mean, what if you were playing a game and the game swapped to a mini game for a minute and then went back to the main game? It would be better to have an ActorManager for the main game and mini game so you wouldn't have to reload everything in the main game when you swapped back from the mini game.

Anyway, just something to think about.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this