0

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

4

2 回答 2

6

你忘了把类限定符放在init

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

无时无刻不在发生。

于 2011-05-05T19:31:51.550 回答
0

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.

于 2011-05-05T19:35:15.197 回答