0

C++ で困っています。SDL を使用してグラフィックを処理するゲームのエンジン クラスを作成しています。Engine クラスは (できれば正しく実装されている) 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

エンジン.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 : 行が含まれています

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

どんな助けでもいただければ幸いです

4

2 に答える 2

6

クラス修飾子を に付けるのを忘れましたinit:

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

いつも起こります。

于 2011-05-05T19:31:51.550 に答える
0

次のように定義initします。

int init(int screenWidth, int screenHeight) {

screenただし、これはグローバルスコープで関数を定義します(変数はありません)。

代わりに、次のように記述します。

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

クラスの関数を定義します。

于 2011-05-05T19:35:15.197 に答える