1
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.

2
  • 1
    I guess you mean game.attachEvent(Event::EventType::Closed, close);
    – DimChtz
    Commented Apr 24, 2018 at 17:40
  • 2
    Just use function name without (), you can add & for readability - &close but that is optional in this case
    – Slava
    Commented Apr 24, 2018 at 17:40

2 Answers 2

3

Instead of close() pass it using close, so game.attachEvent(Event::EventType::Closed, close)

2

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)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.