Montreal
Montreal

Reputation: 2623

Tricky bugs in refacored pong from sfml examples

I am learning SFML by pong example.

Original code can be found there. I am trying to put this lines in a function and then make a call:

    sf::RenderWindow window(
        sf::VideoMode( gameWidth, gameHeight, 32 ),
        "SFML Pong",
        sf::Style::Titlebar | sf::Style::Close
    );
    window.setVerticalSyncEnabled( true );

sf::RenderWindow
CreateWindow( int width, int height, int color_depth, std::string title ){
    sf::RenderWindow window( sf::VideoMode( width, height, color_depth ), title );
    window.setVerticalSyncEnabled( true );
    return window;
}
//....
sf::RenderWindow window = CreateWindow( gameWidth, gameHeight, 32, "My SFML pong" );

And getting a full bunch of non-obvious errors. What am I doing wrong?

Upvotes: 0

Views: 99

Answers (1)

Hiura
Hiura

Reputation: 3530

The first error tells you everything you need to know:

In file included from /usr/include/SFML/System/Lock.hpp:32:0,
             from /usr/include/SFML/System.hpp:36,
             from /usr/include/SFML/Window.hpp:32,
             from /usr/include/SFML/Graphics.hpp:32,
             from main.cpp:5:
/usr/include/SFML/System/NonCopyable.hpp: In copy constructor ‘sf::Window::Window(const sf::Window&)’:
/usr/include/SFML/System/NonCopyable.hpp:67:5: error: ‘sf::NonCopyable::NonCopyable(const sf::NonCopyable&)’ is private NonCopyable(const NonCopyable&);

A Window is, by inheritance, NonCopyable. Therefore you cannot return it from a function.

A solution consist in taking a window reference as input of your function:

void CreateWindow( sf::RenderWindow& window, int width, int height, int color_depth, std::string title )

Upvotes: 1

Related Questions