简体   繁体   English

另一个'x未在此范围内声明'

[英]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: 编写一些代码,我从g ++收到此错误:“实体未在此范围内声明”,在此上下文中:

#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: 这是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. 如果我使用std::vector<class Entity>而不是std::vector<Entity>它可以工作。

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. Entity.h取出#include "GameManager.h" ,因为你实际上并不需要在那个标题中。 (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 . 考虑一下当你包含Entity.h时会发生什么:有些人认为它包含GameManager.h ,后者又包含Entity.h At this point, Entity.h already has its header guard defined, so it skips the contents. 此时, Entity.h已经定义了其头文件,因此它会跳过内容。 Then the parsing of GameManager.h continues, where upon it runs into Entity , and rightfully complains it is not defined. 然后GameManager.h的解析继续,在它运行到Entity ,理所当然地抱怨它没有被定义。 (Indeed, this is still the process of including GameManager.h in the first inclusion of Entity.h , far before Entity is defined!) (事实上,这仍然是其中的过程GameManager.h在第一包含的Entity.h ,远远之前Entity的定义!)

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. Entity位于Psyco2D命名空间中。 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. 假设第一个片段是GameManager.h一部分, GameManager.h您具有循环标头依赖性。 I believe you can fix this by changing the GameManager.h include in Entity.h to class GameManager; 我相信你可以通过将GameManager.h中的GameManager.h包含Entity.hclass GameManager;来解决这个class GameManager; instead. 代替。

Additionally as GMan noted, Entity is in a namespace and you need to qualify Entity with the namespace name. 此外,正如GMan所指出的,Entity位于命名空间中,您需要使用命名空间名称限定Entity。

删除Psyco2D命名空间,它将工作而不声明“类实体”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM