0

これのエラーがわかりません。std::functions を使用してメンバー関数を引数として渡そうとしています。4番目と最後のケースを除いて、正常に動作します。

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);
}

の最初の 3 つの使用法ではエラーはありませんがstd::function、最終的な使用法では次のようになります。

Error 1 error C2064: term does not evaluate to a function taking 0 argumentsの1149行目functional

他のすべてのものを取り出し、さまざまな組み合わせで問題を引き起こしたのはそれだけだったので、エラーが回線で発生していることだけを知っています。

4

1 に答える 1

1

それらのどれもコンパイルするべきではありません。メンバー関数は特別です: オブジェクトが必要です。したがって、2 つの選択肢があります。それらをオブジェクトにバインドするか、オブジェクトを取得させるかです。

// 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;

後者の 2 つは、次のように呼び出すことができます。

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

functionそれは最終的に、あなたがそれをやりたい方法の s のユースケースに依存します-しかし、どちらの方法windowでも、どこかに間違いなく必要です!

于 2015-01-06T03:03:42.910 に答える