Anywho3
Anywho3

Reputation: 1

error: ‘screen’ was not declared in this scope

I'm having troubles with C++. I am making an Engine class for my game that handles graphic using SDL. The Engine class is (hopefully correctly implemented) Singleton.

engine.h:

#ifndef H_ENGINE
#define H_ENGINE

#ifndef H_SDL
#include "SDL/SDL.h"
#endif

class Engine {
public:
    static Engine *getInstance(); //This returns the singleton object of class
    int init(int screenWidth, int screenHeight); //must initialize before use
    ~Engine(); //destructor

private:
    Engine(); //private constructor
    static Engine *instance; //stores the single instance of the class
    SDL_Surface *screen; //Struct for SDL
};

#endif

engine.cpp:

#include "engine.h"

Engine *Engine::instance = NULL;

Engine::Engine() {
    screen = NULL;
}

Engine *Engine::getInstance() {
if(instance == NULL)
    instance = new Engine();

return instance;
}

int init(int screenWidth, int screenHeight) {
SDL_Init(SDL_INIT_EVERYTHING);

//This line has the error: error: ‘screen’ was not declared in this scope
screen = SDL_SetVideoMode(screenWidth, screenHeight, 32, SDL_SWSURFACE);

return 1;
}

Engine::~Engine() {
SDL_Quit();
}

main.cpp: contains the line

Engine::getInstance()->init(600, 400);

Any help would be appreciated

Upvotes: 0

Views: 2432

Answers (2)

Lindydancer
Lindydancer

Reputation: 26094

You define init as:

int init(int screenWidth, int screenHeight) {

However, this defines a function in global scope (there there is no screen variable).

Instead, if you write:

int Engine::init(int screenWidth, int screenHeight) {

You will define the function of your class.

Upvotes: 0

Fred Larson
Fred Larson

Reputation: 62063

You forgot to put the class qualifier on init:

int Engine::init(int screenWidth, int screenHeight)

Happens all the time.

Upvotes: 6

Related Questions