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

Sig*_*gel 2 c++ compiler-errors

这是我的第一个问题.

编写一些代码,我从g ++收到此错误:"在此范围内未声明实体",在此上下文中:

#ifndef Psyco2D_GameManager_
#define Psyco2D_GameManager_

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

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

#endif
Run Code Online (Sandbox Code Playgroud)

这是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
Run Code Online (Sandbox Code Playgroud)

如果我使用std::vector<class Entity>而不是std::vector<Entity>它的工作.

为什么?

感谢所有=)

GMa*_*ckG 7

问题是你有一个循环依赖.取出#include "GameManager.h"Entity.h,因为你并不真正需要它这个头.(向上推荐这个答案,首先指出了这一点.)

注意警卫实际上是问题所在; 但不要把它们拿出来!您只需要最小化包含的内容,并在可能的情况下声明(而不是定义)类型.考虑一下当你包括时会发生什么Entity.h:正如它所包含的一些点GameManager.h,其中包括Entity.h.此时,Entity.h已经定义了其标头保护,因此它会跳过内容.然后解析GameManager.h继续,在它遇到的地方Entity,并且正确地抱怨它没有被定义.(事实上​​,这仍然是包含GameManager.h在第一次包含的过程中Entity.h,远远超出Entity定义!)

请注意,您的大量编辑演示了发布实际代码而不是重新合成代码的重要性.您需要真实的细节来获得真实的答案.


旧:

EntityPsyco2D命名空间中.您需要指定:

class GameManager{
private:
    std::vector<Psyco2D::Entity> entities;
};
Run Code Online (Sandbox Code Playgroud)