【发布时间】:2014-04-15 15:52:17
【问题描述】:
我收到此错误,我有一个类 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_ */
一个 Class 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")));
现在我在“敌人”错误之前得到预期的类型说明符
在game.h中
s_pInstance = new Game();
错误未定义对 `Game::Game()' 的引用
【问题讨论】:
-
在您的问题中指定确切的错误消息。就像它没用一样(尽管您发布了代码)。
-
你确定
SDLGameObject是在全局命名空间中声明的吗? -
头文件相互包含(循环)时会出现此错误。您是否在其他任何标题中这样做?可能是沿线某处(可能在
Game.h中)包含Enemy.h标头,因此包含在SDLGameObject.h中 -
是的,我想我正在这样做!但我不知道是哪一个
-
@user3531442 如果是,那么一个简单的解决方案是将
Enemy.h中的#include "SDLGameObject.h"替换为class SDPGameObject。见This question