2015-01-06 56 views
0

我不明白这个错误。我试图用std::function来传递一个成员函数作为参数。它工作正常,除了在第四和最后一种情况。功能:术语不评估功能错误1149

void window::newGame() { 

} 
//show options 
void window::showOptions() { 

} 
void window::showHelp() { 

} 
//Quits program 
void window::quitWindow() { 
    close(); 
} 
void window::createMenu() { 

    std::function<void()> newGameFunction = std::bind(&window::newGame); 

    std::function<void()> showOptionsFunction = std::bind(&window::showOptions); 


    std::function<void()> showHelpFunction = std::bind(&window::showHelp); 


    std::function<void()> quitWindowFunction = std::bind(&window::quitWindow); 
} 

std::function第3个用法没有错误,但在决赛中使用我得到如下:

Error 1 error C2064: term does not evaluate to a function taking 0 arguments上的functional 1149线。

我只知道错误发生在线上,因为我拿出了所有其他的,这是唯一一个导致任何问题的各种组合。

+0

除非前3个函数是静态的,否则它们都不应该工作。成员函数需要指向类的对象的指针。不知道你的代码是干什么的,我会说试试这个:'... = std :: bind(&window :: quitWindow,this)' –

+0

嗯......我猜这只是没有显示错误。谢谢你的工作! – Matt

回答

1

这些都不应该编译。成员函数是特殊的:它们需要一个对象。所以你有两个选择:你可以将它们与一个对象绑定,或者你可以让它们接受一个对象。

// 1) bind with object 
std::function<void()> newGameFunction = std::bind(&window::newGame, this); 
                  // ^^^^^^ 
std::function<void()> showOptionsFunction = std::bind(&window::showOptions, this); 

// 2) have the function *take* an object 
std::function<void(window&)> showHelpFunction = &window::showHelp; 
std::function<void(window*)> quitWindowFunction = &window::quitWindow; 

后两种可以称之为像:

showHelpFunction(*this); // equivalent to this->showHelp(); 
quitWindowFunction(this); // equivalent to this->quitWindow(); 

这最终取决于你的使用情况为你想这样做哪种方式function秒 - 但无论哪种方式,你一定需要一个window在那里的某个地方!

+0

第一个解决方案奏效。 – Matt