来自 sfml 示例的重构 pong 中的棘手错误

Tricky bugs in refacored pong from sfml examples

我正在通过乒乓球示例学习 SFML。

可以找到原始代码there。我试图将这些行放在一个函数中,然后进行调用:

    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" );

并得到一大堆不明显的 errors。我做错了什么?

第一个错误告诉你你需要知道的一切:

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 通过继承是 NonCopyable。因此你不能从一个函数中 return 它。

一个解决方案是将 window 引用作为函数的输入:

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