我有一个枚举,我已经将其用于多种其他目的,但是在这些情况下,我将枚举用作类的公共变量,这意味着我可以像
EntityManager.FAll
一样访问它。
但是,在新的用例中,我想使用该类中的 Enum 作为另一个类函数的函数参数,例如 CEntityManager::EBroadcastTypes
。
但无论我尝试什么,当我尝试编译代码时,总是失败告诉我,在使用作用域运算符时,我需要使用类或命名空间,即使这是一个类(错误代码:C2653),或者
EBroadcastTypes
不是已知标识符(错误代码:2061)。
作为示例,只是为了进一步“可视化”这一点。 我想在光线投射时使用这个枚举来“过滤通道”,这样我就可以只检查特定的想要的实体。
EntityManager.h
class CEntityManager
{
public:
enum EBroadcastTypes
{
FAll,
FVehicle,
FProjectile,
FDynamic, // Any Entity that can movement on update (ie. Vehicle and Shells)
FStatic, // Any Entity that never moves (ie. Scenery)
};
struct BroadcastFilter
{
EBroadcastTypes type;
vector<string> channels;
};
vector<BroadcastFilter> m_BroadcastFilters;
vector<string> GetChannelsOfFilter(EBroadcastTypes Type)
{
for (const auto& BroadcastFilter : m_BroadcastFilters)
{
if (BroadcastFilter.type == Type)
{
return BroadcastFilter.channels;
}
}
}
}
Main.h
// Primary Update(tick) function
Update()
{
if()// On Some condition
{
TFloat32 range = 400.0f
HitResult Hit = RayCast(ray, EntityManager.FAll, range);
// Do something with Hit ...
}
}
RayCast 文件不包含类,只是属于同一类别的函数,这些函数将在整个代码中跨多个点/类使用。
CRayCast.h
#include "EntityManager.h"
struct CRay
{
CVector3 m_Origin;
CVector3 m_Direction;
};
HitResult RayCast(CRay ray, CEntityManager::EBroadcastType Type, TFloat32 Range);
CRayCast.cpp
#include "CRayCast.cpp"
// Actually using EntityManager here, other than for getting the Enum.
extern CEntityManager EntityManager;
HitResult RayCast(CRay, CEntityManager::EBroadcastTypes Type, TFloat32 Range)
{
// Do the raycasting here
// ...
}
那么有什么方法可以像这样实际使用 Enum 吗?
尝试包含标头以及前向声明类和枚举(编译器然后告诉我它不知道这些前向是什么)。
枚举是 C++ 中类的成员,而不是任何特定对象(它们本质上是静态的)。
您需要使用范围运算符是
::
。正如CEntityManager::FAll
。