我有以下代码:
#include <iostream>
#include <vector>
#include <functional>
using namespace std;
using namespace std::placeholders;
typedef std::vector<std::function<void(void)>> func_list;
class Thing {
private:
func_list flist;
void _sayHi() {
cout << "hi" << endl;
}
void _sayNum(int num) {
cout << num << endl;
}
public:
Thing();
void sayHi();
void sayNum(int num);
void executeFunctions();
};
void Thing::sayHi() {
flist.push_back(
std::bind(&Thing::_sayHi, this) // Unsure of the correct usage here
);
}
void Thing::sayNum(int num) {
flist.push_back(
std::bind(&Thing::_sayNum, this, num) // Unsure of the correct usage here
);
}
void Thing::executeFunctions() {
for (unsigned int i = 0; i < flist.size(); i++) {
flist.at(i)();
}
}
int main() {
Thing thing = Thing();
thing.sayHi();
thing.sayNum(5);
thing.executeFunctions();
}
我的目标是在调用函数时存储函数,以便以后执行它们。我可以使用std::bind(&functionName, param1, param2)
绑定非成员函数,但在函数内部不再有效,使用std::bind(&Class::ClassMemberFunctionName, this)
会给我留下unresolved externals
错误:
错误1错误LNK2019:未解析的外部符号&#34; public:__ thishisall Thing :: Thing(void)&#34; (?? 0Thing @@ QAE @ XZ)在函数_main
中引用错误2错误LNK1120:1个未解析的外部
绑定此类函数的正确方法是什么?
答案 0 :(得分:3)
您缺少Thing::Thing()
构造函数的实现。