0

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?

user2725919
  • 197
  • 1
  • 4
  • 15

3 Answers3

1

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++.

Max Play
  • 3,717
  • 1
  • 22
  • 39
0

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!

Skiller Dz
  • 897
  • 10
  • 17
  • Better don't use int. UGroundDirection groundDirection = UGroundDirection::BOTTOM; is perfectly ok and fits new enum class syntax. – Alex Zaharov May 26 '20 at 01:01
  • @AlexZaharov Both actually work perfectly, and I don't the inconvenient of using either of them. But in general, `int` is used to extract data from an `enum` – Skiller Dz May 26 '20 at 15:31
  • This way is supposed bad and old. Mixing int and enum, that's why "enum class" was introduced. In new projects I do enum class to force stop that hidden conversion. It makes more problems then solves when using optimizing compiler (any new). I can imagine cases when logic will break. For example, int is 16 bit on 8 or 16 bit machines (microcontrollers) while enum constant may have big values. Any way, my point is, better avoid that fake convenience as good practice without real need. – Alex Zaharov May 26 '20 at 15:39
  • @AlexZaharov I agree with you in this case. – Skiller Dz May 26 '20 at 15:40
  • But using both is correct. However, I suggest using `int32_t` or `int64_t` if you really want to use `int` as whole. – Skiller Dz May 26 '20 at 15:42
0

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
    };
};
exkulo
  • 1
  • 4