c++ 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
: 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
如何解决此问题?
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>
{
};
In the other project which imports this DLL I have this error
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
How can I solve this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
解决方案似乎是这样的(从模板类中删除 PST_OBJECT_RECOGNITION_API):
The solution appears to be this (removing PST_OBJECT_RECOGNITION_API from the template class):
模板化函数是否在 DLL 的任何位置实例化?
请记住,模板定义是在实例化时生成的,当涉及到类时,编译器会生成类定义(内存布局等),但如果没有显式使用它们,它可能会选择不生成所有方法。
尝试告诉编译器通过 Now 显式实例化该函数,
因为它是模板化的并且标记为内联,所以最好将其定义在标头中。
Is the templated function being instantiated anywhere on the DLL?
Remember that template definitions are generated on instantiation, when it comes to classes the compiler generates the class definition(memory layout and such), but it may choose not to generate all the methods if they are not being explicitly used.
Try telling the compiler to explicitly instantiate the function via
Now since it is templated and marked
inline
, it is probably best that it be defined in the header.