C++ c++;dll模板(链接器错误) 模板 类PST_对象识别_API测试 { 公众: T; 内联布尔运算符==(常量测试和其他) { 返回t==other.t; } }; 类PST\u对象识别\u API测试\u int :公开考试 { };
在导入此DLL的另一个项目中,我有此错误C++ c++;dll模板(链接器错误) 模板 类PST_对象识别_API测试 { 公众: T; 内联布尔运算符==(常量测试和其他) { 返回t==other.t; } }; 类PST\u对象识别\u API测试\u int :公开考试 { };,c++,templates,dll,linker,C++,Templates,Dll,Linker,在导入此DLL的另一个项目中,我有此错误 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 : pu
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>
{
};
错误3错误LNK2019:未解析的外部符号“\uuu declspec(dllimport)public:bool\uuu thiscall test::operator==(类测试常量&)”(\uuu imp×8$test@H@@QAE_NABV0@@Z)在函数\u main.obj中引用
如何解决此问题?解决方案似乎是这样的(从模板类中删除PST_OBJECT_RECOGNITION_API):
模板
课堂测试
{
公众:
T;
内联布尔运算符==(常量测试和其他)
{
返回true;
}
};
类PST\u对象识别\u API测试\u int
:公开考试
{
};
模板化函数是否在DLL的任何位置实例化
请记住,模板定义是在实例化时生成的,当涉及到类时,编译器会生成类定义(内存布局等),但如果没有显式使用这些方法,则可能会选择不生成所有方法
尝试告诉编译器通过
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>
{
};
template bool test::operator==(const test&);
现在,由于它是模板化的,并且标记为
inline
,所以最好在标题中定义它。想想看,想想看,我的答案可能有点误导(因此我删除了它)。一个想法是,当您编写“testx=newtest\u int”时,客户机代码中会发生什么?
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>
{
};
template bool test<int>::operator==(const test<int> &);