2011-02-17 64 views
0
template <class T> 
class PST_OBJECT_RECOGNITION_API test 
{ 
public: 
    T t; 

    inline bool operator==(const test & other) 
    { 
     return t == other.t; 
    } 
}; 

class PST_OBJECT_RECOGNITION_API test_int 
    : public test<int> 
{ 
}; 

在其中進口該DLL的其他項目,我有這樣的錯誤C++ DLL模板(連接錯誤)

Error 3 error LNK2019: unresolved external symbol "__declspec(dllimport) public: bool __thiscall test<int>::operator==(class test<int> const &)" ([email protected]@@[email protected]@Z) referenced in function _main main.obj 

我怎樣才能解決這個問題?

回答

0

的解決方案似乎是這個(從模板類刪除PST_OBJECT_RECOGNITION_API):

template <class T> 
class test 
{ 
public: 
    T t; 

    inline bool operator==(const test<T> & other) 
    { 
     return true; 
    } 
}; 

class PST_OBJECT_RECOGNITION_API test_int 
    : public test<int> 
{ 
}; 
+0

想到了它,我的答案可能有點誤導(所以我刪除了它)。一個想法,當你寫「test x = new test_int」時,你的客戶端代碼會發生什麼? – Jimmy 2011-02-17 19:25:44

0

正在對DLL的任何地方實例化的模板函數?

請記住,在實例化時生成模板定義,當涉及到編譯器生成類定義(內存佈局等)的類時,但如果它們未被明確使用,它可能會選擇不生成所有方法。

試着告訴編譯器通過

template bool test<int>::operator==(const test<int> &); 

顯式實例化的功能現在,因爲它是模板inline,它可能是最好的,它在標題中定義。