发送带有指向对象成员函数的指针的参数?

时间:2017-02-04 01:52:16

标签: c++ function pointers parameter-passing member

我正在尝试创建一个类,队列的up函数在经过一定时间后运行。该类将采用地址指针,指向该成员函数对象的指针,并且还允许(当前)单个参数。

错误的代码:

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

这一行来自上面

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

我被认为试图将参数传递给函数指针是个问题。我试图使void(C :: * func)(A)期望与上一个参数相同类型的数据。当我删除模板A并停止尝试传递参数时,我没有编译错误。

这是我得到的编译错误:

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

任何帮助都会非常感激!

编辑:如果我遗漏任何重要信息,请告诉我,我会更快地更新。

0 个答案:

没有答案