0

我真的不知道如何解释这一点。

我在命名空间中有 2 个类 World 和 Entity:

阿尔忒弥斯::系统;

和一个类组件

artemis::组件;

我已经遇到了 World 和 Entity 的问题,在尝试编译时它说它们中的任何一个都不是声明的类型。(它们都相互包含)所以在两个类中,在命名空间中,我添加了一个前向声明。这似乎可以解决问题。

现在,当我尝试在 Component.h 中包含“世界”时,它给了我以下错误:

组件不是命名空间名称

我的 Component 类位于命名空间 artemis::component 中。

这真让我抓狂。我真的不明白是什么导致了这个错误。

头组件.h

#ifndef _COMPONENT_H_
#define _COMPONENT_H_

#include <bitset>
#include "BitSize.h"
#include "World.h"
#include <unordered_map>
#include <typeinfo>
#include <string>
#include <iostream>
#include <assert.h>
//#include "EntityManager.h"

using namespace std;
using namespace artemis::system;

namespace artemis {
    namespace component {


        class Component {
            public:
                virtual ~Component() = 0;
            protected:
                Component(){};
        };


        /**
         * Identifies a bitset and id for a component object
         * Do not instantiate a ComponentType, instead use the ComponentTypeManager.
         * */
        class ComponentType {

            public:
                ComponentType();

                bitset<BITSIZE> getBit() const;
                int getId() const;
            private:

                static bitset<BITSIZE> nextBit;
                static int nextId;

                bitset<BITSIZE> bit;
                int id;
                void init();

        };


//Surpress unused variable warnning. Might need to rewrite it
//#pragma GCC diagnostic push
//#pragma GCC diagnostic ignored  "-Wunused-variable"
        /**
         * Manages the id and bitset for every component based on their type.
         * */

        class ComponentTypeManager {

            private:
                ComponentTypeManager();
                static unordered_map<size_t,ComponentType*> componentTypes;



        public:


                /**
                 *
                 **/
                static ComponentType & getTypeFor(const type_info &t);

                /**
                * Gets the component type object
                **/
                template<typename c>
                static ComponentType & getTypeFor() {

                    //Check if we are being legal with components and shizzle
                    //Component * c = (component*)0;

                    assert((std::is_base_of< Component, c >::value == true));

                    return getTypeFor(typeid(c));
                }

                /**
                * Gets the bit set of a component
                **/
                template<typename c>
                static bitset<BITSIZE> getBit() {

                    //Check if we are being legal with components and shizzle
                    //Component * c = (component*)0;

                    assert((std::is_base_of< Component, c >::value == true));

                    ComponentType & type = getTypeFor(typeid(c));
                    return type.getBit();
                }
                /**
                 * Gets the component id
                 **/
                template<typename c>
                static int getId() {

                    //Check if we are being legal with components and shizzle

                    assert((std::is_base_of< Component, c >::value == true));

                    ComponentType & type = getTypeFor(typeid(c));
                    return type.getId();
                };



                //typedef getCompBit bitset<BITSIZE>(*getBit<Component>)();

        };
//#pragma GCC diagnostic pop




        /*template<typename T>
        class ComponentMapper {

            private:
                //ComponentType * type;
                EntityManager * em;

            public:

                ComponentMapper(World &world) {
                    em = world.getEntityManager();
                    //type = ComponentTypeManager::getTypeFor<T>();
                }

                ~ComponentType() {
                    type = nullptr;
                    em = nullptr;
                }

                T & get(Entity * e) {
                    return &(T)em->getComponent<T>(e);
                }

        };*/

    };
};

#endif

标头实体.h

#ifndef _ENTITY_H
#define _ENTITY_H

#include <bitset>
#include <string>
#include <cstddef>
#include <typeinfo>
#include "BitSize.h"
#include "Component.h"
#include "ImmutableBag.h"
#include "World.h"
//#include "EntityManager.h"

using namespace artemis::util;
using namespace artemis::component;
//using namespace artemis;
using namespace std;



namespace artemis {
    namespace system {
        class World;
        class Entity {

            private:
                int id;
                long int uniqueId;
                bitset<BITSIZE> typeBits;
                bitset<BITSIZE> systemBits;
                World * world;
                //EntityManager * entityManager;
            protected:

            public:
                Entity(World * world, int id);
                ~Entity();
                int getID();
                void setUniqueId(long int uniqueId);
                long int getUniqueID();

                bitset<BITSIZE> getTypeBits();
                void addTypeBit(bitset<BITSIZE> bit);
                void removeTypeBit(bitset<BITSIZE> bit);
                bitset<BITSIZE> getSystemBits();
                void addSystemBit(bitset<BITSIZE> bit);
                void removeSystemBit(bitset<BITSIZE> bit);
                void setSystemBits(bitset<BITSIZE> systemBits);
                void setTypeBits(bitset<BITSIZE> typeBits);
                void reset();

                string toString();

                void addComponent(Component * c);

                //Might change to non template
                template<typename c>
                void removeComponent() {
                    //entityManager->removeComponent(this,ComponentTypeManager::getTypeFor<c>());
                }

                void removeComponent(ComponentType & type);
                bool isActive();

                Component * getComponent(ComponentType & type);

                /*template<typename c>
                Component * getComponent() {
                    return (c)entityManager->getComponent(ComponentTypeManager.getTypeFor<c>());
                }*/

                ImmutableBag<Component*> * getComponents();
                void refresh();
                void remove();
                void setGroup(string group);
                void setTag(string tag);


        };
    };
};
#endif // $(Guard token)

标头世界.h

#ifndef _WORLD_H_
#define _WORLD_H_

//#include "SystemManager.h"
//#include "EntityManager.h"
#include "ImmutableBag.h"
#include "Entity.h"

using namespace artemis::util;



namespace artemis {
    namespace system {

    class Entity;

        class World {
            public:
                World();
                ~World();
                //SystemManager * getSystemManager();
                //EntityManager * getEntityManager();
                //TagManager *   getTagManager();
                //GroupManager * getGroupManager();
                int getDelta();
                void setDetla(int delta);
                void deleteEntity(Entity *e);
                void refreshEntity(Entity *e);
                //Entity* createEntity();
                //Entity* getEntity(int entityID);
                void loopStart();



            private:
                //SystemManager * systemManager;
                //EntityManager * entityManager;
                //TagManager * tagManager;
                //GroupManager * grouManager;
                int delta;
                Bag<Entity*> refreshed;
                Bag<Entity*> deleted;



        };
    };
};
#endif // $(Guard token)
4

1 回答 1

1

您的问题是您需要Component已经定义才能创建World,并且您需要World被定义才能创建Component。这是不可能的; 每个人的定义都需要另一个人。

您需要重组您的类,以便您不需要ComponentWorld' 标头中定义,或者您不需要WorldComponent' 标头中定义。您始终可以将它们包含在 .cpp 中,但不能包含在标头中。这是一个循环包含问题。您还可以前向声明其中一个类。

您本质上是在说:A除非我已经创建,否则我无法创建BB除非我已经创建,否则我无法创建A。这是不可能的,编译器就是这么告诉你的。

于 2012-07-02T23:43:01.370 回答