C++不是命名空间

C++ is not a namespace

本文关键字:命名空间 C++      更新时间:2023-10-16

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

我在命名空间中有 2 个类世界和实体:

阿尔忒弥斯::系统;

和一个类组件

阿尔忒弥斯::组件;

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

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

组件不是命名空间名称

我的组件类驻留在命名空间 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)

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

你需要重组你的类,这样你要么不需要在World的标头中定义Component,要么不需要在Component的标头中定义World。 您始终可以在.cpp中包含它们,但不能包含在标头中。 这是一个循环包容问题。 您还可以向前声明其中一个类。

你本质上是在说:除非我已经创造了B,否则我无法创造A;除非我已经创建了A,否则我无法创建B。 这是不可能的,编译器会告诉你。