1

Suppose I have a code in C++ SFML as follows;

void eventManager(sf::RenderWindow window)
{
    event evnt;
    window.pollEvent(evnt);
    if (evnt.type == sf::Event::Closed)
        window.close();
}

int main()
{
    sf::RenderWindow window(sf::VideoMode(500, 500), "Window");
    eventManager(window);
}

when I try to compile something like this, the debugger spits out an error message saying "attempting to reference a deleted function". I understand now that a sf::RenderWindow cannot be passed to another function, so what would be a better way to do this?

EvilDuck
  • 63
  • 6
  • 1
    Your code is trying to pass the window by copy rather than by pointer or by reference. Presumably copying a RenderWindow is not allowed. – TheUndeadFish Jul 21 '18 at 05:32
  • 3
    `void eventManager(sf::RenderWindow window)` ==> `void eventManager(sf::RenderWindow& window)` – alter_igel Jul 21 '18 at 05:47
  • 3
    And it's not true that a `sf::RenderWindow` cannot be passed to a function, it simply cannot be passed _by value_, because that would create a copy, and the implementation prevents that. – alter_igel Jul 21 '18 at 05:49

1 Answers1

3

Alter Igel told me how to do it in the comments, so I thought I'd post it as an answer so this can be closed (Thanks dude!)

I needed to declare the function like this;

 eventManager(sf::RenderWindow& window)

I assume the & lets the compiler know that it is referencing the window, not copying it. Again, credit to Alter Igel.

EvilDuck
  • 63
  • 6
  • 1
    No problem, friend. If you're not completely sure what a reference is and why it's different from passing by value, you should probably learn more theory from a [recommended C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) before taking on a large project with a library like SFML. – alter_igel Jul 21 '18 at 06:31