Pregunta

I get this error i have a class Enemy and a class Player and a Class SDLGameObject

Error: expected class-name before '{' token It's on line 15 at location Enemy : public SDLGameObject {

class Enemy

/*
 * 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_ */

class Player

/*
 * 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_ */

class 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_ */

i also have an abstract class 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_ */

a Class LoaderParams to load teh parameters of the different objects

/*
 * 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_ */

also a class Game

* 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_ */

and finally a class 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_ */

where am i placing my includes wrong? I don't seem to figure this out. Sometimes the error dissapears by Player and appears by enemy and the way around.

In Class game.cpp

Error:

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")));

now i get expected type-specifier before 'Enemy' error

In game.h

s_pInstance = new Game();

Error undefined reference to `Game::Game()'

¿Fue útil?

Solución

Game.h and SDLGameObject.h include each other. So, to correct that, replace #include "SDLGameObject.h" with class SDLGameObject in your Game.h file.

If you are calling the class in Game.cpp include your SDLGameObject.h header there instead of in Game.h because you will not have access to it any other way. Also, for good measure, move the inclusion of Game.h to your SDLGameObject.cpp definition file.

EDIT: You may also want to include Enemy.h in your Game.cpp file. This would be true for any class that your program states you don't have that type specified after your header include switches.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top