Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Can't compile when using handleEvents method of the Window class  (Read 489 times)

0 Members and 1 Guest are viewing this topic.

masooli

  • Newbie
  • *
  • Posts: 2
    • View Profile
Can't compile when using handleEvents method of the Window class
« on: February 18, 2025, 05:15:58 pm »
Hi everyone,

This is my first project using C++ and SFML 3.0!

I followed the getting started guide to create a new project in Visual Studio, and I successfully opened a window and drew a shape without any issues.

However, when I tried to use the handleEvents method for handling events instead of polling for them, I encountered an error message. Here’s what I’ve done so far:


#include <SFML/Graphics.hpp>

#include <SFML/Graphics.hpp>

class EventHandler
{
public:
        std::optional<std::string> operator()(const sf::Event::Closed& event) {
                return "Closed\r\n";
        }

        std::optional<std::string> operator()(const sf::Event::KeyPressed& event) {
                return "Key pressed\r\n";
        }

        std::optional<std::string> operator()(const sf::Event::MouseButtonPressed& event) {
                return "Mouse pressed\r\n";
        }

        template <typename T>
        std::optional<std::string> operator()(const T&) {
                return "Unhandled event\r\n";
        }
};

int main()
{
        sf::RenderWindow window(sf::VideoMode({ 800, 600 }), "My window");
        EventHandler eventHandler;

        while (window.isOpen())
        {
                window.handleEvents(
                        [&](const sf::Event::Closed& event) { eventHandler(event); },
                        [&](const sf::Event::KeyPressed& event) { eventHandler(event); }
                );
                window.display();
        }
}

 

The compiler shows the following error message:


in function &#39;void __thiscall sf::WindowBase::handleEvents<class `int __cdecl main(void)&#39;::`4&#39;::<lambda_1>,class `int __cdecl main(void)&#39;::`4&#39;::<lambda_2> >(class `int __cdecl main(void)&#39;::`4&#39;::<lambda_1> &&,class `int __cdecl main(void)&#39;::`4&#39;::<lambda_2> &&)&#39; buffer &#39;overloadSet&#39; of size 12 bytes will be overrun; 4 bytes will be written starting at offset 9

 

I'm not sure what I'm doing wrong. Any help or guidance would be greatly appreciated!
« Last Edit: February 18, 2025, 05:30:09 pm by masooli »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11145
    • View Profile
    • development blog
    • Email
Re: Can't compile when using handleEvents method of the Window class
« Reply #1 on: February 19, 2025, 08:37:43 am »
The handleEvents is still quite new to me as well. If I understood the documentation correctly, you should be able to pass the eventHandler instance directly, as you have operator() overloads for the different types, so you don't need to use lambdas that then call the handler.

Personally, I'd probably go with some event/input handler that just defines different functions for the different event types and then have the lambda call those functions.

Something like the following code, but that's really just a preference (also untested code).

class InputHandler
{
public:
    void handleKeyPress(sf::RenderWindow& window, const sf::Event::KeyPressed& event)
    {
        switch (event.code) {
            case sf::Keyboard::Key::Escape:
                window.close();
                break;
            case sf::Keyboard::Key::Space:
                std::cout << "Space key pressed!" << std::endl;
                break;
            default:
                std::cout << "Key pressed: " << static_cast<int>(event.code) << std::endl;
                break;
        }
    }
};

int main() {
    auto window = sf::RenderWindow{sf::VideoMode{{800, 600}}, "SFML Window"};
    auto inputHandler = InputHandler{};

    while (window.isOpen())
    {
        window.handleEvents(
            // Window close handler
            [&window](const sf::Event::Closed&)
            {
                window.close();
            },
            // Key press handler - using member function through lambda
            [&window, &inputHandler](const sf::Event::KeyPressed& event)
            {
                inputHandler.handleKeyPress(window, event);
            });
    }
}
 
Official FAQ: https://www.sfml-dev.org/faq/
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

masooli

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Can't compile when using handleEvents method of the Window class
« Reply #2 on: February 19, 2025, 05:23:01 pm »
I don't think I can build a project with this feature! Even the example code in the repo can't be compiled without error (and it gives the same error but for more event handlers). Right now, I will stick to the poll method.
Thank you.


The handleEvents is still quite new to me as well. If I understood the documentation correctly, you should be able to pass the eventHandler instance directly, as you have operator() overloads for the different types, so you don't need to use lambdas that then call the handler.

Personally, I'd probably go with some event/input handler that just defines different functions for the different event types and then have the lambda call those functions.

Something like the following code, but that's really just a preference (also untested code).

class InputHandler
{
public:
    void handleKeyPress(sf::RenderWindow& window, const sf::Event::KeyPressed& event)
    {
        switch (event.code) {
            case sf::Keyboard::Key::Escape:
                window.close();
                break;
            case sf::Keyboard::Key::Space:
                std::cout << "Space key pressed!" << std::endl;
                break;
            default:
                std::cout << "Key pressed: " << static_cast<int>(event.code) << std::endl;
                break;
        }
    }
};

int main() {
    auto window = sf::RenderWindow{sf::VideoMode{{800, 600}}, "SFML Window"};
    auto inputHandler = InputHandler{};

    while (window.isOpen())
    {
        window.handleEvents(
            // Window close handler
            [&window](const sf::Event::Closed&)
            {
                window.close();
            },
            // Key press handler - using member function through lambda
            [&window, &inputHandler](const sf::Event::KeyPressed& event)
            {
                inputHandler.handleKeyPress(window, event);
            });
    }
}
 

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11145
    • View Profile
    • development blog
    • Email
Re: Can't compile when using handleEvents method of the Window class
« Reply #3 on: February 19, 2025, 10:22:24 pm »
Even the example code in the repo can't be compiled without error (and it gives the same error but for more event handlers).
Huh, which one?

Have you made sure to enable C++17?

Official FAQ: https://www.sfml-dev.org/faq/
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

 

anything