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

Author Topic: "Attempting to reference a deleted function." when returning a sf::RenderWindow  (Read 8801 times)

0 Members and 1 Guest are viewing this topic.

Enyss

  • Newbie
  • *
  • Posts: 4
    • View Profile
Hello,

 I have a strange error when I try to return a RenderWindow reference from a singleton class:

 Error C2280 'sf::RenderWindow::RenderWindow(const sf::RenderWindow &)': attempting to reference a deleted function.

Minimal code :

#include <SFML\Graphics.hpp>

class RenderManager
{
public:
        RenderManager();
        static RenderManager & rm();

        sf::RenderWindow & getWindow();
private:
        sf::RenderWindow window;
};

RenderManager::RenderManager()
{
        window.create(sf::VideoMode(800, 600), "Realms of Dream");
        tileSet.loadFromFile("gfx/tileset.png");
}

RenderManager & RenderManager::rm()
{
        static RenderManager rm;
        return rm;
}

sf::RenderWindow & RenderManager::getWindow()
{
        return window;
}

Thanks.
« Last Edit: December 29, 2015, 07:50:36 pm by Enyss »

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
That's what happen when you try to copy something that ain't copyable.
SFML / OS X developer

Enyss

  • Newbie
  • *
  • Posts: 4
    • View Profile
In fact, I return a reference (I made a copy mistake in the code I pasted here), so it should works no?

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Can you provide a minimal complete example?
Also tell us where the error occurs.

The idea of it is also to have 1:1 copyable code, so that such mistakes can't happen :)
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Enyss

  • Newbie
  • *
  • Posts: 4
    • View Profile
This is a minimal code that show the error:

#include <SFML\Graphics.hpp>

class RenderManager
{
public:
        RenderManager();
        static RenderManager & rm();

        sf::RenderWindow & getWindow();
private:
        sf::RenderWindow window;
};

RenderManager::RenderManager()
{
        window.create(sf::VideoMode(800, 600), "Realms of Dream");
}

RenderManager & RenderManager::rm()
{
        static RenderManager rm;
        return rm;
}

sf::RenderWindow & RenderManager::getWindow()
{
        return window;
}

int main()
{
        sf::RenderWindow window = RenderManager::rm().getWindow();
        return 0;
}
 

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
sf::RenderWindow window = RenderManager::rm().getWindow();
This is copying the RenderWindow from rm to the newly constructed window.

EDIT: If you are storing the windows in the manager and accessing them, don't copy them, reference them by creating a reference instead.
i.e.
sf::RenderWindow& window = RenderManager::rm().getWindow();
« Last Edit: December 30, 2015, 12:12:28 am by Hapax »
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Enyss

  • Newbie
  • *
  • Posts: 4
    • View Profile
Oh thanks... I missed this -_-'