{token SDL错误之前的预期类名

时间:2014-04-15 15:52:18

标签: c++ sdl

我得到这个错误我有一个类Enemy和一个类Player和一个类SDLGameObject

错误: expected class-name before '{' token它位于第{15}位置Enemy : public SDLGameObject {

类敌人

/*
 * Enemy.h
 *
 *  Created on: 15 apr. 2014
 *      Author: JAN
 */

#ifndef ENEMY_H_
#define ENEMY_H_

#include "SDLGameObject.h"

class Enemy : public SDLGameObject {
public:
    Enemy(const LoaderParams* pParams);

     virtual void draw();
     virtual void update();
     virtual void clean();
};

#endif /* ENEMY_H_ */

班级球员

/*
 * Player.h
 *
 *  Created on: 15 apr. 2014
 *      Author: JAN
 */

#ifndef PLAYER_H_
#define PLAYER_H_


#include "SDLGameObject.h"

class Player : public SDLGameObject {
public:
     Player(const LoaderParams* pParams);

     virtual void draw();
     virtual void update();
     virtual void clean();

};

#endif /* PLAYER_H_ */

类SDLGameObject

/*
 * SDLGameObject.h
 *
 *  Created on: 15 apr. 2014
 *      Author: JAN
 */

#ifndef SDLGAMEOBJECT_H_
#define SDLGAMEOBJECT_H_

#include "GameObject.h"
#include "TextureManager.h"
#include "Game.h"

class SDLGameObject : public GameObject {
public:
     SDLGameObject(const LoaderParams* pParams);
     virtual void draw();
     virtual void update();
     virtual void clean();

protected:
     int m_x;
     int m_y;

     int m_width;
     int m_height;

     int m_currentRow;
     int m_currentFrame;

     std::string m_textureID;

};

#endif /* SDLGAMEOBJECT_H_ */

我还有一个抽象类GameObject

/*
 * GameObject.h
 *
 *  Created on: 15 apr. 2014
 *      Author: JAN
 */

#ifndef GAMEOBJECT_H_
#define GAMEOBJECT_H_

#include "LoaderParams.h"
#include <string>
using namespace std;


class GameObject {
public:

    virtual void draw() = 0;
    virtual void update() = 0;
    virtual void clean() = 0;


protected:

     GameObject(const LoaderParams* pParams) {}
     virtual ~GameObject() {}
};

#endif /* GAMEOBJECT_H_ */

一个类LoaderParams来加载不同对象的参数

/*
 * LoaderParams.h
 *
 *  Created on: 15 apr. 2014
 *      Author: JAN
 */

#ifndef LOADERPARAMS_H_
#define LOADERPARAMS_H_



#include <string>
using namespace std;

class LoaderParams {
public:

     LoaderParams(int x, int y, int width, int height, std::string   textureID) : m_x(x), m_y(y), m_width(width), m_height(height),   m_textureID(textureID)
    {

    }

     int getX() const { return m_x; }
     int getY() const { return m_y; }
     int getWidth() const { return m_width; }
     int getHeight() const { return m_height; }
     std::string getTextureID() const { return m_textureID; }

private:

     int m_x;
     int m_y;
     int m_width;
     int m_height;
     std::string m_textureID;

};

#endif /* LOADERPARAMS_H_ */

也是一个类游戏

* Game.h
 *
 *  Created on: 15 apr. 2014
 *      Author: JAN
 */

#ifndef GAME_H_
#define GAME_H_

#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <vector>
#include "TextureManager.h"
#include "Gameobject.h"
#include "SDLGameobject.h"





class Game {
public:

    static Game* Instance()
    {
        if(s_pInstance == 0)
        {
            s_pInstance = new Game();
            return s_pInstance;
        }
        return s_pInstance;
    }

    //simply set the running variable to true
    bool init(const char* title, int xpos, int ypos, int width, int height, int flags);
    void render();
    void update();
    void handleEvents();
    void clean();

    SDL_Renderer* getRenderer() const { return m_pRenderer; }




    std::vector<GameObject*> m_gameObjects;


    //a function to access private running variable

    bool running() {return m_bRunning; }

private:

     Game(); // create the s_pInstance member variable
     static Game* s_pInstance;


    SDL_Window* m_pWindow;
    SDL_Renderer* m_pRenderer;

    int m_currentFrame;
    bool m_bRunning;
};
typedef Game TheGame;
#endif /* GAME_H_ */

最后是一个类TextureManager

/*
 * TextureManager.h
 *
 *  Created on: 15 apr. 2014
 *      Author: JAN
 */

#ifndef TEXTUREMANAGER_H_
#define TEXTUREMANAGER_H_

#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <string>
#include <map>
using namespace std;



class TextureManager {
public:

    static TextureManager* Instance()
     {
        if(s_pInstance == 0)
        {
            s_pInstance = new TextureManager();
            return s_pInstance;
        }
        return s_pInstance;
    }

    bool load(std::string fileName,std::string id,  SDL_Renderer* pRenderer);

    void draw(std::string id, int x, int y, int width, int  height, SDL_Renderer* pRenderer, SDL_RendererFlip flip =  SDL_FLIP_NONE);

    void drawFrame(std::string id, int x, int y, int width, int  height, int currentRow, int currentFrame, SDL_Renderer*  pRenderer, SDL_RendererFlip flip = SDL_FLIP_NONE);

    std::map<std::string, SDL_Texture*> m_textureMap;



private:
    TextureManager() {}
    TextureManager(const TextureManager&);
      TextureManager& operator=(const TextureManager&);

      static TextureManager* s_pInstance;


};


typedef TextureManager TheTextureManager;
#endif /* TEXTUREMANAGER_H_ */

我把包含错误放在哪里?我似乎无法解决这个问题。有时,错误会被玩家消失,并由敌人和周围的方式出现。

Class game.cpp

错误:

m_gameObjects.push_back(new Player(new LoaderParams(100, 100, 128, 82, "animate")));
m_gameObjects.push_back(new Enemy(new LoaderParams(300, 300, 128, 82, "animate")));

现在我在“Enemy&#39;之前得到预期的类型说明符”。错误

在game.h中

s_pInstance = new Game();

错误未定义对'Game :: Game()&#39;

的引用

1 个答案:

答案 0 :(得分:1)

Game.hSDLGameObject.h互相包含。因此,要更正此问题,请将#include "SDLGameObject.h"替换为class SDLGameObject文件中的Game.h

如果您在Game.cpp中呼叫该课程,请在SDLGameObject.h中添加Game.h标题,而不是Game.h,因为您无法以任何其他方式访问该标题。另外,为了更好地衡量,请将SDLGameObject.cpp包含在Enemy.h定义文件中。

编辑:您可能还想在Game.cpp文件中加入{{1}}。对于您的程序声明您在标题包含开关后未指定该类型的任何类都适用。