0

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 the 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!

SU3
  • 5,064
  • 3
  • 35
  • 66
nontoon
  • 3
  • 1
  • 2
    Can you edit this into a [mcve]? – NathanOliver Jun 29 '17 at 18:34
  • Possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – AndyG Jun 29 '17 at 18:34
  • 5
    This is another Most Vexing Parse issue... `ptr` is a function, not a variable. – 1201ProgramAlarm Jun 29 '17 at 18:35
  • 1
    @1201ProgramAlarm -- yes, `ptr` is declared to be the name of a function. I don't see how this is the Most Vexing Parse. Its argument list consists solely of the name of a type. That makes it a function declaration. Nothing subtle here. – Pete Becker Jun 29 '17 at 19:09

1 Answers1

3

The problem seems to be that you wrote

std::shared_ptr<T> ptr(T);

instead of

std::shared_ptr<T> ptr(value);
SU3
  • 5,064
  • 3
  • 35
  • 66