I am trying to implement template singleton pattern but I got the following error. How should I overcome this error?
Below is singleton_template.h
#include <iostream>
#include <string>
/*
* Burada yapılan herşey ".h" dosyası içerinde olmalı
*/
template<typename T>
class Singleton
{
private:
static T* _instance;
Singleton(const Singleton&);
Singleton & operator=(const Singleton& rhs);
protected:
Singleton();
public:
static T* getInstance()
{
if(!_instance)
_instance = new T;
return _instance;
}
void destroyInstance()
{
delete _instance;
_instance = NULL;
}
};
template<typename T>
T* Singleton<T>::_instance= NULL;
Here is the singleton_template.cpp
#include <iostream>
#include <string>
#include "singleton_template.h"
class GKM : public Singleton<GKM>
{
public:
GKM()
{
std::cout << "Single GKM created"<<std::endl;
}
virtual void initialize(){return;}
};
class Turnstile: public GKM
{
public:
Turnstile(){std::cout << "Single turnstile created add: "<< this<<std::endl;}
virtual void initialize(){std::cout <<"Turnstile"<< std::endl;}
};
int main()
{
GKM* trn= Turnstile::getInstance();
trn->initialize();
return 0;
}
Here is what I get after a failed compilation:
/tmp/ccfD0Xgx.o: In function
GKM::GKM()
: singleton_template.cpp:(.text._ZN3GKMC2Ev[_ZN3GKMC5Ev]+0xd): undefined reference toSingleton<GKM>::Singleton()
collect2: error: ld returned 1 exit status