jeudi 29 juin 2017

C++ linker error when calling template function that is defined in header

I'm getting a linking error when calling a template function, but the linker error does not seem to point to the template function. It seems to point to the shared pointer in the function. My class with the template function:

class Blackboard {
    public:
    template<class T>
    bool setValue(std::string key, T* value) {
        std::shared_ptr<T> ptr(T);
        boost::any v = ptr;
        auto it = map.insert(std::pair<std::string, boost::any>(key, v));
        return it.second;
      }
}

Most similar questions say put all implementation in the header, which I have done. The linking error happens in the class that calls this function. Example class that calls blackboard:

class Example {
    void foo(Blackboard* blackboard) {
        int* i = new int;
        *i = 0;
        blackboard->setValue<int>("some key", i);
    }
}

The error:

Error LNK2019 unresolved external symbol "class std::shared_ptr<int> __cdecl ptr(int)" (?ptr@@YA?AV?$shared_ptr@H@std@@H@Z) referenced in function "public: bool __cdecl BehaviorTree::Blackboard::setValue<int>(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,int *)" (??$setValue@H@Blackboard@BehaviorTree@@QEAA_NV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@PEAH@Z)

Both the Blackboard class and Example class are part of a visual studio static lib project. The linking error occurs when compiling a program using the static library.

I'm having difficulty finding a solution to this problem, so any help would be appreciated!

Aucun commentaire:

Enregistrer un commentaire