K
kathy
I try to implement the Singleton for my Driver. The code piece:
/////////////////////////
MySingleton.h
/////////////////////////
template <class T> class CSingleton
{
public:
static T* GetInstance();
protected:
CSingleton();
~CSingleton();
private:
CSingleton(CSingleton const&);
CSingleton& operator=(CSingleton const&);
static T* m_pInstance;
};
/////////////////////////
MySingleton.cpp
/////////////////////////
#include "StdAfx.h"
#include "MySingleton.h"
template <class T> T* CSingleton<T>::m_pInstance=NULL;
template <class T> T* CSingleton<T>::GetInstance()
{
if(!m_pInstance)
m_pInstance = new T;
assert(m_pInstance !=NULL);
return m_pInstance;
}
/////////////////////////
Driver.h
/////////////////////////
class CDriver
{
public:
CDriver(void);
~CDriver(void);
int GetData();
private:
int m_nData;
};
/////////////////////////
Driver.cpp
/////////////////////////
#include "StdAfx.h"
#include "Driver.h"
CDriver::CDriver(void):
m_nData(100)
{
}
CDriver::~CDriver(void)
{
}
int CDriver::GetData()
{
return m_nData;
}
In my MFC dialog button routine:
void CMyDlg::OnBnClickedButtonSingleton()
{
int n = 1;
CDriver *pDriver = CSingleton<CDriver>::GetInstance();
n = pDriver->GetData();
}
When I try to build it in studio 2010, I got link error:
MyDlg.obj : error LNK2019: unresolved external symbol "public: static
class CDriver * __cdecl CSingleton<class
CDriver>::GetInstance(void)" (?GetInstance@?
$CSingleton@VCDriver@@@@SAPAVCDriver@@XZ) referenced in function
"public: void __thiscall
CBoostDlg::OnBnClickedButtonSingleton(void)" (?
OnBnClickedButtonSingleton@CBoostDlg@@QAEXXZ)
C:\Temp\Studio 2010\TestDlg\Debug\Test.exe : fatal error LNK1120: 1
unresolved externals
What is wrong?
/////////////////////////
MySingleton.h
/////////////////////////
template <class T> class CSingleton
{
public:
static T* GetInstance();
protected:
CSingleton();
~CSingleton();
private:
CSingleton(CSingleton const&);
CSingleton& operator=(CSingleton const&);
static T* m_pInstance;
};
/////////////////////////
MySingleton.cpp
/////////////////////////
#include "StdAfx.h"
#include "MySingleton.h"
template <class T> T* CSingleton<T>::m_pInstance=NULL;
template <class T> T* CSingleton<T>::GetInstance()
{
if(!m_pInstance)
m_pInstance = new T;
assert(m_pInstance !=NULL);
return m_pInstance;
}
/////////////////////////
Driver.h
/////////////////////////
class CDriver
{
public:
CDriver(void);
~CDriver(void);
int GetData();
private:
int m_nData;
};
/////////////////////////
Driver.cpp
/////////////////////////
#include "StdAfx.h"
#include "Driver.h"
CDriver::CDriver(void):
m_nData(100)
{
}
CDriver::~CDriver(void)
{
}
int CDriver::GetData()
{
return m_nData;
}
In my MFC dialog button routine:
void CMyDlg::OnBnClickedButtonSingleton()
{
int n = 1;
CDriver *pDriver = CSingleton<CDriver>::GetInstance();
n = pDriver->GetData();
}
When I try to build it in studio 2010, I got link error:
MyDlg.obj : error LNK2019: unresolved external symbol "public: static
class CDriver * __cdecl CSingleton<class
CDriver>::GetInstance(void)" (?GetInstance@?
$CSingleton@VCDriver@@@@SAPAVCDriver@@XZ) referenced in function
"public: void __thiscall
CBoostDlg::OnBnClickedButtonSingleton(void)" (?
OnBnClickedButtonSingleton@CBoostDlg@@QAEXXZ)
C:\Temp\Studio 2010\TestDlg\Debug\Test.exe : fatal error LNK1120: 1
unresolved externals
What is wrong?