开发者

c++ dll templates (linker error)

开发者 https://www.devze.com 2023-02-11 07:08 出处:网络
template <class T> class PST_OBJECT_RECOGNITION_API test { public: T t; inline bool operator==(const test & other)
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_A开发者_开发问答PI 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?


The solution appears to be this (removing PST_OBJECT_RECOGNITION_API from the template class):

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>
{
};


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

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

Now since it is templated and marked inline, it is probably best that it be defined in the header.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号