2015-01-06 25 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