开发者

Another 'x was not declared in this scope'

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 =)


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;
};


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜