2011-09-27 85 views
0

好吧,我已經定義了模板類,它按預期編譯,當我在應用程序的CMainFrame的函數中實現此類並編譯它時,我收到未解決的鏈接錯誤。自定義CMDIChildWndEx模板類鏈接錯誤

void CMainFrame::OnFunc() 
{ 
    CTestList<CMyClass> list; 
} 

的鏈接錯誤:

1>mainfrm.obj : error LNK2019: unresolved external symbol "public: virtual __thiscall CTestList<class CMyClass>::~CTestList<class CMyClass>(void)" ([email protected]@@@@[email protected]) referenced in function "protected: void __thiscall CMainFrame::OnFunc(void)" ([email protected]@@IAEXXZ) 
1>mainfrm.obj : error LNK2019: unresolved external symbol "public: __thiscall CTestList<class CMyClass>::CTestList<class CMyClass>(void)" ([email protected]@@@@[email protected]) referenced in function "protected: void __thiscall CMainFrame::OnFunc(void)" ([email protected]@@IAEXXZ) 

我已經檢查了所有的明顯缺失的頭,未定義功能等,但它仍然在我拋出這些錯誤,這些文件是主要的一部分應用程序和不在靜態/共享庫,因爲這是我所期望的錯誤,如果我這樣做..

這裏是模板類的基本定義減少,我遵循我相信成爲構建這個班級的正確途徑,而我的所有研究似乎都表明了這一點正確的。

真的需要得到這個釘子儘快,所以如果你們&女孩可以幫助我會非常感激。

乾杯, 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; 
} 
+0

您的CMainFrame實現文件是否包含CTestList.h(pp)和CMyClass? – flumpb

+0

@kisplit,它將不得不,否則會有編譯器錯誤,而不是鏈接器錯誤。 –

回答

0

模板代碼是不是在頭文件內聯。當正在編譯模板類cpp文件時,編譯器不知道T需要什麼實例。當你的主文件正在被編譯,你需要實例化一個CTestList時,編譯器只有模板頭文件。你需要在你的模板.cpp文件中添加一個強制顯式模板實例 - 所以目前這個編譯器會生成模板的正確的CMyClass實例化。

+0

現貨,現在完全合理,謝謝你的幫助Ricibob :) – DIGGIDY