首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SFML在通过类初始化窗口时出现NonCopyable错误

SFML在通过类初始化窗口时出现NonCopyable错误
EN

Stack Overflow用户
提问于 2013-01-04 10:23:28
回答 2查看 3.1K关注 0票数 3

我正在制作一个Game类,它有一个用来制作窗口的函数。当我试图执行这个函数时,VS2012给出了这个错误:

代码语言:javascript
复制
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 (未完成):

代码语言:javascript
复制
#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 (未完成,但具有测试所需的所有功能):

代码语言:javascript
复制
#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:

代码语言:javascript
复制
#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;
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-01-04 10:31:18

这是一个复制操作,即使您可能打算将其初始化:

代码语言:javascript
复制
GameWindow = sf::RenderWindow(sf::VideoMode( getWindowWidth(), getWindowHeight() ), "Title Goes Here");

而且(如果这一点不明显) sf::RenderWindow是不可复制的。

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

代码语言:javascript
复制
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.使用它。

票数 3
EN

Stack Overflow用户

发布于 2014-11-06 01:25:53

下面的竞赛中的"uninitialized local variable 'event‘vs 2013“怎么样?

代码语言:javascript
复制
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)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14150237

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档