0

I'm using the SFML library and wanted to create a function for events, which worked perfectly fine. until I tried to add a scroll feature using an optional parameter.

my problem simplified

#include <SFML\Graphics.hpp>

int check(sf::RenderWindow&, int& = 0);

int main()
{
    sf::RenderWindow win(sf::VideoMode(800,600), "test");
    win.setFramerateLimit(60);
    
    int scroll;
    
    while(win.isOpen())
    {
        check(win);
        
        win.clear();
        
        win.display();
    }
}

check(sf::RenderWindow& win, int& scroll)
{
    sf::Event event;
    while(win.pollEvent(event))
    {
        if(event.type == sf::Event::Closed)
        {
            win.close();
        }
        if(event.type == sf::Event::MouseWheelMoved)
        {
            scroll += event.mouseWheel.delta*20;    
        }
    }   
}

can someone tell me how to do this while still passing scroll as refrence?

Myth
  • 49
  • 5
  • You cannot bind non-const lvalue reference to an rvalue. So if you need to pass by reference then you have to remove the default argument 0 and call check like: check(win,scroll). An other option would be for the function to return the scroll value. Since check already returns an int, you can return a pair or a custom struct. – Hein Breukers Mar 13 '23 at 13:41
  • You can not increment `+=` the literal value `0`. – Drew Dormann Mar 13 '23 at 13:41
  • Rather than have a default value, have 2 overloads of `check`, one that takes a reference and one that doesn't. The one that doesn't can do this: `int check(sf::RenderWindow& win) { int scroll; return check(win, scroll); }` – Kevin Mar 13 '23 at 13:42

0 Answers0