views:

120

answers:

3

Hi to all, this is my first question here.

Writing some code, i receive this error from g++: "Entity was not declared in this scope", in this context:

#ifndef Psyco2D_GameManager_
#define Psyco2D_GameManager_

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

namespace Psyco2D{
    class GameManager{J
    private:
        std::vector<Entity> entities;
    };
}

#endif

This is the content of Entity.h:

#ifndef Psyco2D_Entity_
#define Psyco2D_Entity_

#include <string>
#include "GameManager.h"
#include "EntityComponent.h"


namespace Psyco2D{

    class Entity{
        friend class GameManager;

    private:
        /* Identificatore */
        std::string _name;

        /* Components list */
        std::map<const std::string, EntityComponent*> components;

    protected:
        Entity(const std::string name);

    public:
        inline const std::string getName() const{
            return this->_name;
        }

        void addComponent(EntityComponent* component, const std::string name);

        EntityComponent* lookupComponent(const std::string name) const;

        void deleteComponent(const std::string name);

    };

}

#endif

If i use std::vector<class Entity> instead of std::vector<Entity> it works.

Why?

Thanks to all =)

+7  A: 

The problem is you have a cyclic dependency. Take out #include "GameManager.h" in Entity.h, since you don't actually need it in that header. (Up-vote this answer, which first pointed it out.)

Note the guards are actually the problem; but don't take them out! You just need to minimize the includes you have, and declare (and not define) types when possible. Consider what happens when you include Entity.h: As some point it includes GameManager.h, which in turn includes Entity.h. At this point, Entity.h already has its header guard defined, so it skips the contents. Then the parsing of GameManager.h continues, where upon it runs into Entity, and rightfully complains it is not defined. (Indeed, this is still the process of including GameManager.h in the first inclusion of Entity.h, far before Entity is defined!)

Note your numerous edits demonstrate why it's important to post real code, not re-synthesized code. You need real details to get real answers.


Old:

Entity is in the Psyco2D namespace. You need to specify that:

class GameManager{
private:
    std::vector<Psyco2D::Entity> entities;
};
GMan
Sorry and sorry again, i miss a part of code... The GameManager class is in Psyco2D namespace too
Sigel
Ehi, it works, thanks for the explanation. I vote Mark B's answer because, as you told, first pointed it out. Thanks Mark B =)
Sigel
A: 

Remove the Psyco2D-namespace and it will work without declaring "class Entity".

Simon
+3  A: 

Assuming the first snippet is part of GameManager.h you have a circular header dependency. I believe you can fix this by changing the GameManager.h include in Entity.h to class GameManager; instead.

Additionally as GMan noted, Entity is in a namespace and you need to qualify Entity with the namespace name.

Mark B
It isn't the problem, i have in GameManager header preprocessor ifndef and define directives like in Entity.h
Sigel
@Sigel: It *is* the problem. :) I walked you through it on my answer.
GMan