'Referencing Enum C++ Unreal

I have created an Enum Class in Unreal C++

#include "GroundDirection.generated.h"

UENUM(BlueprintType)
enum UGroundDirection
{
     BOTTOM = 0,
     LEFT = 1,
     TOP = 2,
     RIGHT = 3
};

In C# or Java I could instantiate a copy of this Enum doing something like this:

GroundDirection groundDirection = GroundDirection.BOTTOM;

I thought I could do something similar with Unreal C++

UGroundDirection groundDirection = UGroundDirection.BOTTOM;

However when I do this I get the following error:

error C2228: left of '.BOTTOM' must have class/struct/union

How to I instantiate Enums in light of this error?



Solution 1:[1]

BOTTOM isn't a class but an integer. Moreover, UGroundDirection isn't a class/struct/union but an enum, and hence it is kind of considered as a namespace. You shall use :: instead of .

To solve your problem, you shall simply remove UGroundDirection from UGroundDirection groundDirection = UGroundDirection.BOTTOM; and replace it with: int groundDirection = UGroundDirection::BOTTOM

That's all!

Solution 2:[2]

I have created an Enum Class in Unreal C++

No, you didn't. You just created a C-style enum.
Also, the UE++ coding standard states that an enum should have an E as a prefix.

So your declaration should actually look like this:

#include "GroundDirection.generated.h"

UENUM(BlueprintType)
enum class EGroundDirection
{
     BOTTOM = 0,
     LEFT = 1,
     TOP = 2,
     RIGHT = 3
};

To access the members of the enum, you access them like they are static members of a class:

EGroundDirection direction = EGroundDirection::BOTTOM;

This is, because you are not accessing a member of an instance, but a declaration which is always done by using :: in C++.

Solution 3:[3]

In C++, you can use either the "namespace-enum" style or "enum class" style. Both of them will limit the elements in specific domain.

Well, some comments say I didn't proveide the solution.

You can try

#include "GroundDirection.generated.h"

UENUM(BlueprintType)
enum class EGroundDirection
{
     BOTTOM = 0,
     LEFT = 1,
     TOP = 2,
     RIGHT = 3
};

or

#include "GroundDirection.generated.h"

UENUM(BlueprintType)
namespace EGroundDirection
{
    enum EGroundDirection
    {
         BOTTOM = 0,
         LEFT = 1,
         TOP = 2,
         RIGHT = 3
    };
};

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Skiller Dz
Solution 2 Max Play
Solution 3