[英]c++ dll templates (linker error)
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的另一個項目中,我遇到此錯誤
Error 3 error LNK2019: unresolved external symbol "__declspec(dllimport) public: bool __thiscall test<int>::operator==(class test<int> const &)" (__imp_??8?$test@H@@QAE_NABV0@@Z) referenced in function _main main.obj
我怎么解決這個問題?
解決方案似乎是這樣(從模板類中刪除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>
{
};
模板化函數是否可以在DLL的任何位置實例化?
請記住,模板定義是在實例化時生成的,當涉及到類時,編譯器會生成類定義(內存布局等),但如果未顯式使用它們,則它可能選擇不生成所有方法。
嘗試告訴編譯器通過顯式實例化該函數
template bool test<int>::operator==(const test<int> &);
現在,由於已將其模板化並標記為inline
,因此最好在標頭中對其進行定義。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.