Custom CMDIChildWndEx template class linking errors
Ok, I have defined the template class, which compiles as expected, when I implement this class in a function of the CMainFrame of the application and compile it, I receive unresolved linking errors.
void CMainFrame::OnFunc()
{
CTestList<CMyClass> list;
}
The linking errors:
1>mainfrm.obj : error LNK2019: unresolved external symbol "public: virtual __thiscall CTestList<class CMyClass>::~CTestList<class CMyClass>(void)" (??1?$CTestList@VCWnd@@@@UAE@XZ) referenced in function "protected: void __thiscall CMainFrame::OnFunc(void)" (?OnFunc@CMainFrame@@IAEXXZ)
1>mainfrm.obj : error LNK2019: unresolved external symbol "public: __thiscall CTestList<class CMyClass>::CTestList<class CMyClass>(void)" (??0?$CTestList@VCWnd@@@@QAE@XZ) referenced 开发者_StackOverflow中文版in function "protected: void __thiscall CMainFrame::OnFunc(void)" (?OnFunc@CMainFrame@@IAEXXZ)
I've checked all the obvious missing headers, undefined functions, etc, but still it throws these errors at me, the files are all part of the main application and are not in static/shared libs, as this is the error I would expect if i'd done so..
Here is the basic definition of the template class cut right down, I've followed what I believe to be the correct path in constructing the class, and all my research seems to suggest its correct.
Really need to get this nailed ASAP, so if you guys & girls could help I would be very grateful.
Cheers, DIGGIDY
/////////////////////////////////////////////////////////////////////////////
// CTestList class
template <class T>
class CTestList : public CMDIChildWndEx
{
//DECLARE_DYNAMIC(CTestList<T>)
public:
CTestList();
virtual ~CTestList();
protected:
// Generated message map functions
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
DECLARE_MESSAGE_MAP()
};
/////////////////////////////////////////////////////////////////////////////
// CTestList
//IMPLEMENT_DYNCREATE(CTestList<SDCM_OBJECT_TYPE>, CMDIChildWndEx)
template <class T>
CTestList<T>::CTestList()
{
}
template <class T>
CTestList<T>::~CTestList()
{
}
BEGIN_TEMPLATE_MESSAGE_MAP(CTestList, T, CMDIChildWndEx)
ON_WM_CREATE()
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTestList message handlers
template <class T>
int CTestList<T>::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if ( CMDIChildWndEx::OnCreate(lpCreateStruct) == -1 )
return -1;
// this removes the crappy un-drawn client edge on screen
ModifyStyleEx(WS_EX_OVERLAPPEDWINDOW, WS_EX_WINDOWEDGE);
return 0;
}
Your template code is not inlined in the header file. When the template class cpp file is being compiled the compiler doesn't know what instances of T will be required. When your main file is being compiled and you need to instantiate a CTestList the compiler only has the template header file. You need to add a force explicite template instantiation to your template .cpp file - so at the moment this is compiled it will generation the correct CMyClass instantiation of the template.
精彩评论