gpt4 book ai didi

C++ 不是命名空间

转载 作者:行者123 更新时间:2023-11-28 08:01:39 25 4
gpt4 key购买 nike

我真的不知道怎么解释。

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

artemis::system;

和一个类组件

artemis::component;

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

现在,当我尝试在 Component.h 中包含“world”时,出现以下错误:

component is not a namespace-name

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

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

Header 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来定义创建组件。这是不可能的;每个人的定义都需要另一个人。

你需要重构你的类,这样你就不需要在 World 的头部定义 Component,或者你不需要 World 定义在 Component 的头部。您始终可以将它们包含在 .cpp 中,但不能包含在 header 中。这是一个循环包含问题。您也可以前向声明其中一个类。

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

关于C++ 不是命名空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11302551/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com