【问题标题】:SFML Giving NonCopyable Error when Initializing a Window Via ClassSFML 通过类初始化窗口时出现不可复制错误
【发布时间】:2012-12-18 11:36:57
【问题描述】:

我正在制作一个带有创建窗口的函数的 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));
    }
}

Main.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;
}

【问题讨论】:

  • 我猜你应该使用指针。
  • 这就是我的想法,但如何?
  • 假设 main.cpp 中的 Game game; 而不是 Game.h 中的 static GAME Game;

标签: c++ sfml


【解决方案1】:

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

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-&gt; 而不是 GameWindow. 使用它。

【讨论】:

  • 也可以调用create成员函数,其参数与构造函数相同。
  • @BenjaminLindley ...您只需要继续破坏它:D
  • 哇,谢谢伙计!那行得通!现在我得想办法初始化分辨率值了哈哈……我自己试试
  • 呃……什么?我不明白。
  • @Tetramputechture 您可以简单地使用GameWindow.create(sf::VideoMode( getWindowWidth(), getWindowHeight() ), "Title Goes Here"); 而无需更改任何其他内容。我不知道create 方法,所以我想出了一些更复杂的方法。
【解决方案2】:

接下来的比赛中“未初始化的局部变量'event' vs 2013”​​怎么样

while (window.isOpen()){
            sf::Event event;
            while (event.type)
            {
                if (event.type == sf::Event::Closed)
                    window.close();
                //case haddle all other case
                switch (event.type)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-11
    • 1970-01-01
    • 2020-09-15
    • 1970-01-01
    相关资源
    最近更新 更多