Search code examples
c++macrosinclude-guards

Error "not declared in the scope" but with interfaces


You see I've been trying to create a std::vector which contains the Entity class inside the IState class. Both classes are interfaces.

The error is

'Entity' was not declared in this scope

and it points to :

protected:
    std::vector <Entity*> ent_map;

inside IState.h

I've been trying for hours now to solve it. Once I made a forward declaration inside IState.h but once I did and tried to use the vector it spews out that it's an incomplete class, so I was back to square one.

Any ideas?

Entity.h

#ifdef __ENTITY__
#define __ENTITY__

#include <iostream>
#include <SDL.h>

class Entity
{
    public:
    virtual ~Entity();
    virtual void load(const char* fileName, std::string id, SDL_Renderer* pRenderer) = 0;
    virtual void draw() = 0;
    virtual void update() = 0 ;
    virtual void clean() = 0;

    /*void int getX() { return m_x;}
    void int getY() { return m_y;}
    void std::string getTexID {return textureID;}
    */


};


#endif // __ENTITY__

IState.h

#ifndef IState_
#define IState_

#include "Entity.h"
#include <vector>

class IState
{
    public :
    virtual ~IState();
    virtual void update() = 0;
    virtual void render(SDL_Renderer* renderTarget) = 0;
    virtual bool onEnter() = 0;
    virtual bool onExit() = 0;
    virtual void handleEvents(bool* gameLoop,SDL_Event event) = 0;
    virtual void resume() = 0;
    virtual std::string getStateID() = 0;
    virtual void setStateID(std::string id) = 0;

    protected:
        std::vector <Entity*> ent_map;

};


#endif // IState_

Solution

  • The content of "Entity.h" won't be included at all.

    Change

    #ifdef __ENTITY__
    

    to

    #ifndef __ENTITY__
    

    BTW: The name contains double underscore or begins with an underscore followed by an uppercase letter is reserved in C++, you need to be careful about it.