Tetramputechture
Tetramputechture

Reputation: 2921

SFML Giving NonCopyable Error when Initializing a Window Via Class

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

Upvotes: 3

Views: 3084

Answers (2)

user110036
user110036

Reputation: 1

how about "uninitialized local variable 'event' vs 2013" in following contest

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)

Upvotes: 0

Andrei Tita
Andrei Tita

Reputation: 1236

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. .

Upvotes: 3

Related Questions