我想将成员函数绑定到std::function<void(void)>
. 我听说成员函数需要一个额外的参数,即实例指针。因此我调用std::bind(&Class::Function, this, parameter)
但是当我执行函数对象时,它会引发运行时错误。
Application.exe 中 0x748D4B32 处未处理的异常:Microsoft C++ 异常:内存位置 0x0114F4E8 处的 std::bad_function_call。
该参数是指向我自己struct
的一个的指针。我怎么做错了?您还需要哪些额外信息?
更新:这是我的代码。
class ModuleRenderer
{
struct Pass{ std::function<void()> Function; /* many more members... */ };
std::vector<std::pair<std::string, Pass>> passes;
enum Drawfunc{ FORMS, SKY, LIGHTS, QUAD, SCREEN };
void AddPass(std::string Name, Drawfunc Function)
{
Pass pass;
// set some of the members
// ...
passes.push_back(std::make_pair(Name, pass));
Pass *pointer = &(passes.back().second);
switch (Function)
{
case FORMS:
pointer->Function = std::bind(&ModuleRenderer::DrawForms, this, pointer);
break;
// analogeously for the other cases
// ...
}
}
void DrawForms(Pass *pass)
{
// ...
}
// is called consecutively after adding all passes
void Update()
{
for(auto i : passes)
// some initializing based on members of pass
i.Function();
}
};