函数指针不工作 (C++)

Function pointer not working (C++)

void close() {
    //game.close();
}
int main(int argc, char** argv) {
    game.display();
    game.attachEvent(Event::EventType::Closed, close());
    while (game.isOpen()) {
        game.render();
    }
    return 0;
}

error: cannot initialize a parameter of type 'void (*)()' with an rvalue of type 'void' game.attachEvent(Event::EventType::Closed, close());

note: passing argument to parameter here
void attachEvent(sf::Event::EventType, void (*)());

为什么会显示这个错误?我正在尝试附加一个我可以用 function(); 调用的事件。作为参数,声明为void (*function)().
谢谢

而不是 close() 使用 close 传递它,所以 game.attachEvent(Event::EventType::Closed, close)

传递 close() 调用关闭函数并传递结果 void

您想传递函数本身:

game.attachEvent(Event::EventType::Closed, close);

(注意 close 上没有括号)