本文介绍了通过类初始化窗口时,SFML给出不可复制的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作一个Game类,它有一个窗口。当我尝试执行该函数,VS 2012给我这个错误:

 错误1错误C2248:'sf :: NonCopyable: :operator =':无法访问在'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;
};

静态游戏

#endif

Game.cpp(未完成,具有所有必要的功能用于测试):

  #includeGame.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> 
#includeGame.h

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

while(Game.GameWindow.isOpen())
{
sf ::事件事件;
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 $ c> class,或者你可以使它成为一个动态对象:

  std :: unique_ptr< sf :: RenderWindow> GameWindow; //你正在使用VS2012所以C ++ 11智能指针是最好的方法这样做

//...skipped一些代码
GameWindow = std :: unique_ptr< sf :: RenderWindow>(new sf :: RenderWindow(sf :: VideoMode(getWindowWidth(),getWindowHeight()),Title Goes Here));

,然后通过 GameWindow-> 而不是 GameWindow。


I'm making a Game class with a function that makes a window. When I try to execute the function, VS 2012 gives me this error:

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

Here is my Game.h (unfinished):

#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 (unfinished, has all the necessary functions for testing, though):

#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;
}
解决方案

This is a copy operation, even though you probably intended it to be initialization:

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

And (in case that wasn't obvious) sf::RenderWindow is non-copyable.

You can initialize the RenderWindow via its constructor in the constructor of your GAME class instead, or you can make it a dynamic object:

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

and then use it via GameWindow-> rather than GameWindow. .

这篇关于通过类初始化窗口时,SFML给出不可复制的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 01:17