Search code examples
c++animationeventsfunction-pointerssfml

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 (*)());

Why is it displaying this error? I am trying to attach an event which I can call with function();. As a parameter, it is declared as void (*function)().
Thanks.


Solution

  • Passing close() calls the close function and passes the result which is void

    You want to pass the function itself with:

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

    (Note there are no parenthesis on close)