简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM