传递函数指针时未解析的外部符号



我正在尝试创建一个通用按钮,我希望能够在单击它时传递要调用的函数。

主.cpp:

#include "Window.h"
int testfunction() {
print("Test");
return 1;
}
...other stuff...
window->SetButtonFunction_NoArgs<int>(" *unrelated stuff* ", &testfunction);

window 是一个 WindowMain 类:

窗口.h:

class WindowMain {
*Stuff*
template <class R1> void SetButtonFunction_NoArgs(string id, R1(*func)());
*Other Stuff*
};

窗口.cpp:

///Find the object of that id and set the fuction to it
template <class R1> void WindowMain::SetButtonFunction_NoArgs(string id,  R1(*func)()) {
for (int i = 0; i < objects_.size(); i++) {
if (objects_[i].getId() == id) {
objects_[i]->AddButton_NoArgs<R1>(func);
}
}
}

我认为问题出在某个地方,但我找不到它......当我尝试编译时,它给了我这个:

1>main.obj : error LNK2019: unresolved external symbol "public: void __cdecl WindowMain::SetButtonFunction_NoArgs<int>(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,int (__cdecl*)(void))" (??$SetButtonFunction_NoArgs@H@WindowMain@@QEAAXV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@P6AHXZ@Z) referenced in function "void __cdecl Create(void)" (?Create@@YAXXZ)

感谢您的帮助!

模板方法必须在头文件中定义。如果不是模板,您可以将定义移动到.cpp文件(不过您希望包含 window.h(

最新更新