0

I'm trying to make a class that queue's up functions to run after a certain amount of time has passed. The class will take a pointer of an address, a pointer to the object of that member function, and also allow up to (currently) a single parameter.

The code that errors:

cfs.taskManager.addTask(500, &GameState::print, cfs.getCurrentState(), temp);
//500: (int) time in ms converted later.
//&GameState::print: void GameState::print(std::string), takes a string argument.
// cfs.getCurrentState(): returns pointer to print object.
//temp: std::string

TaskManager.hpp:

class TaskManager
{
public:
    TaskManager();
    ~TaskManager();

    // float ms: time in milliseconds before event occurs.
    // void(C::*f)(): direct pointer to function.
    // C* o: direct pointer to object that the function is attached onto.
    // A a: Data type copy of spare argument.
    template <typename C, typename A>
    void addTask(int ms, void(C::*func)(A), C* o, A a);



    //  Dump all queued tasks.
    void clearTasks();
    //  Check if program time has passed currTime+delay of the 
    void run();

    std::vector<Task> task;
    sf::Time time;
};

This line here from above

void addTask(int ms, void(C::*func)(A), C* o, A a);

I'm led to believe that trying to pass an argument into the function pointer is the issue. I tried to make void(C::*func)(A) expect the same type of data as last parameter. When I remove template A and stop trying to pass an argument, I get no compilation errors.

Here's the compilation error I get:

error LNK2001: unresolved external symbol "public: void __thiscall TaskManager::addTask<class GameState,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >(int,void (__thiscall GameState::*)(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >),class GameState *,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (??$addTask@VGameState@@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@TaskManager@@QAEXHP8GameState@@AEXV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@ZPAV1@0@Z)1  >C:\Users\Honor\documents\visual studio 2015\Projects\CFS\Release\CFS.exe : fatal error LNK1120: 1 unresolved externals

Any help at all would be very appreciated!

Edit: If I'm leaving out any crucial information, let me know I'll update really fast.

Honor
  • 63
  • 2
  • 9
  • The full definition of the template function must be defined in the header file. A declaration is insufficient. See the duplicate question for more information. – Sam Varshavchik Feb 04 '17 at 01:58
  • The answer should be "You have to provide the full definition of the template function before calling it". Linked question's title is misleading and seems unrelated, answering question why, while here the question is what. – ivan_onys Feb 04 '17 at 02:36

0 Answers0