У меня есть Enum, который я уже использую для множества других целей, однако в этих случаях я использую Enum как общедоступную переменную класса, что означает, что я могу получить к нему доступ как
Код: Выделить всё
EntityManager.FAll
However, in the new use case I have for it I want to use the Enum in that class as a function parameter of another class's function like
Код: Выделить всё
CEntityManager::EBroadcastTypes
But no matter what I try, when I try to compile the code this always fails telling me that either when using the scope operator I need to be using a Class or Namespace even though this is a class (error code: C2653), or that
Код: Выделить всё
EBroadcastTypes
Just to further 'visualize' this, as an example.
I want to use this Enum for 'filtering channels' when Ray Casting, so that I can either only check for specific Wanted Entities.
Код: Выделить всё
EntityManager.h
class CEntityManager
{
public:
CEntityManager();
~CEntityManager();
private:
// Prevent use of copy constructor and assignment operator (private and not defined)
CEntityManager( const CEntityManager& );
CEntityManager& operator=( const CEntityManager& );
/////////////////////////////////////
// Public interface
public:
enum EBroadcastTypes
{
FAll,
FTank,
FShell,
FDynamic, // Any Entity that can movement on update (ie. Tank and Shells)
FStatic, // Any Entity that never moves (ie. Enivronment)
};
struct BroadcastFilter
{
EBroadcastTypes type;
vector channels;
};
vector
m_BroadcastFilters;
vector GetChannelsOfFilter(EBroadcastTypes Type)
{
for (const auto& BroadcastFilter : m_BroadcastFilters)
{
if (BroadcastFilter.type == Type)
{
return BroadcastFilter.channels;
}
}
}
};
Код: Выделить всё
ProjectName.cpp
#include "EntityManager.h"
#include "CRayCasting.h"
CEntityManager EntityManager;
// Primary Update(tick) function
Update()
{
if(true)// On Some condition
{
TFloat32 range = 400.0f
CRay ray;
ray.m_Origin = CVector3(0,0,0);
ray.m_Direction = CVector3(0,0,0);
HitResult Hit = RayCast(ray, EntityManager.FAll, range);
// Do something with Hit ...
}
}
Код: Выделить всё
CRayCasting.h
#include "EntityManager.h"
class CEntity;
struct HitResult
{
CVector3 m_hitPosition;
CEntity* m_hitEntity;
};
struct CRay
{
CVector3 m_Origin;
CVector3 m_Direction;
};
// Simple RayCast, checking intersection with any Entity,
// Checks against all entities that belong to this Channel.
HitResult RayCast(CRay ray, CEntityManager::EBroadcastTypes WantedChannel); // This is line 50 in the output/error messages
Код: Выделить всё
CRayCasting.cpp
#include "CRayCasting.h"
// Get Access to the Entity Manager
extern CEntityManager EntityManager;
// Simple RayCast, checking intersection with any Entity - Checks whether entity is of wanted Type/Channel
HitResult RayCast(CRay ray, CEntityManager::EBroadcastTypes WantedChannel)
{
HitResult closestIntersection;
vector channels = EntityManager.GetChannelsOfFilter(static_cast(WantedChannel));
// Do the Raycast
return closestIntersection;
}
Tried including the header as well as forward declaring both the class and enum (the compiler then told me it didn't know what those forwards are).
EDIT:
I accidentally used the wrong operator (. вместо ::) для вызова функции RayCast, как я написал вопрос.
Но это даже не тот случай, когда компилятор выдает мне ошибку.
Ошибка происходит внутри заголовочного файла при объявлении функции.
Здесь также указаны коды ошибок в лучшем формате:
Код: Выделить всё
Error C2653 'CEntityManager': is not a class or namespace name ProjectName E:\Project\Source\Physics\CRayCasting.h 50
Код: Выделить всё
Error C2061 syntax error: identifier 'EBroadcastTypes' ProjectName E:\Project\Source\Physics\CRayCasting.h 50
As suggested I included pretty much everything of the code relevant to the use of the enum from that class in my function. If there is anything missing to count this as MRE please let me know, my first time doing this.
Additionally added the compiler output as suggested (Not sure if I should do the whole thing or only relevant parts, but here is everything for now):
https://pastebin.com/Sjyg5nKw
Источник: https://stackoverflow.com/questions/781 ... -its-class