c++ - SFML 在通过类初始化窗口时给出不可复制的错误

标签 c++ sfml

我正在创建一个 Game 类,其中包含一个创建窗口的函数。当我尝试执行该函数时,VS 2012 给我这个错误:

Error   1   error C2248: 'sf::NonCopyable::operator =' : cannot access private member declared in class 'sf::NonCopyable'   C:\SFML-2.0-rc\include\SFML\Window\Window.hpp   476 1   Faceman

这是我的 Game.h(未完成):

#ifndef FP_MENU
#define FP_MENU

#include <SFML/Graphics.hpp>

class GAME {
    public:

        sf::RenderWindow GameWindow;

        void createWindow();
            unsigned int getWindowWidth();
            unsigned int getWindowHeight();

            void setWindowWidth(unsigned int w);
            void setWindowHeight(unsigned int h);

        void loadMMenu();

        void startGame( bool isTurboMode );
            void pause();

        void options();
            void changeWindowSize( unsigned int x, unsigned int y );
            void changeVolume( int i );

        void Quit();

    private:
        unsigned int WINDOW_WIDTH;
        unsigned int WINDOW_HEIGHT;
};

static GAME Game;

#endif

Game.cpp(未完成,但具有测试所需的所有功能):

#include "Game.h"

void GAME::setWindowWidth(unsigned int w) {

    w = WINDOW_WIDTH;
}

void GAME::setWindowHeight(unsigned int h) {

    h = WINDOW_HEIGHT;
}

unsigned int GAME::getWindowHeight() {

    return WINDOW_HEIGHT;
}

unsigned int GAME::getWindowWidth() {

    return WINDOW_WIDTH;
}

void GAME::createWindow() {

    if(getWindowHeight() != 0 && getWindowWidth() != 0)
    {
        GameWindow = sf::RenderWindow(sf::VideoMode( getWindowWidth(), getWindowHeight() ), "Title Goes Here");
        GameWindow.setPosition(sf::Vector2i(50, 50));
    }

    else 
    {
        setWindowWidth(1024);
        setWindowHeight(768);
        GameWindow = sf::RenderWindow(sf::VideoMode( getWindowWidth(), getWindowHeight() ), "Title Goes Here");
        GameWindow.setPosition(sf::Vector2i(50, 50));
    }
}

主要.cpp:

#include <SFML/Graphics.hpp>
#include "Game.h"

int main()
{
    Game.createWindow(Game.getWindowWidth(), Game.getWindowHeight());

    while (Game.GameWindow.isOpen())
    {
        sf::Event event;
        while (Game.GameWindow.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                Game.GameWindow.close();
        }

        Game.GameWindow.clear();
        Game.GameWindow.display();
    }

    return 0;
}

最佳答案

这是一个复制操作,即使您可能希望它是初始化:

GameWindow = sf::RenderWindow(sf::VideoMode( getWindowWidth(), getWindowHeight() ), "Title Goes Here");

并且(以防万一)sf::RenderWindow 是不可复制的。

您可以改为在 GAME 类的构造函数中通过其构造函数初始化 RenderWindow,或者您可以将其设为动态对象:

std::unique_ptr<sf::RenderWindow> GameWindow; //you are using VS2012 so C++11 smart pointers are the best way to do this

//...skipped some code
GameWindow = std::unique_ptr<sf::RenderWindow>(new sf::RenderWindow(sf::VideoMode( getWindowWidth(), getWindowHeight() ), "Title Goes Here"));

然后通过 GameWindow-> 而不是 GameWindow. 使用它。

关于c++ - SFML 在通过类初始化窗口时给出不可复制的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14150237/

相关文章:

c++ - LNK2005 - 在 .hpp 文件中声明函数时出错

visual-studio-2010 - 在 Visual Studio 2010 之外运行时游戏崩溃

c++ - 在 Eigen(c++) 中使用 noalias 之前是否需要检查指针

c++ - Texture.loadFromFile 降低了我的 fps。应该是 60 而不是 30fps

c++ - 类析构函数和指针释放

c++ - 不同的值取决于 C++ 类型

c++ - 不在 SFML 中绘制的 Sprite - 从 vector 中存储和检索

c++ - Tilemap 碰撞 SFML C++

c++如何将编译器选项检测为字符串

c++ - 从 char 字符串中提取 2 个数字?