vendredi 26 juillet 2019

Enum Class Operator Overload no match found for operator

I'm attempting to overload some operators for an Enum class. I get a compiler error saying it's unable to find the operator

In Enum.h

enum class SomeEnum : unsigned
{
    Test0 = 0,
    Test1  = (1 << 0),
    Test2  = (1 << 1),
};

In Enum.cpp

#include "Enum.h"
#include <type_traits>
SomeEnum operator|(SomeEnum lhs, SomeEnum rhs)
{
    return static_cast<SomeEnum > (
        static_cast<std::underlying_type<SomeEnum >::type>(lhs) |
        static_cast<std::underlying_type<SomeEnum >::type>(rhs)
    );
}

in main.cpp

#include "Enum.h"
int main()
{
  SomeEnum blah = SomeEnum::Test1 | SomeEnum::Test2; 
}

The compiler spits out an error saying: no match for 'operator|' (operand types are 'SomeEnum ' and 'SomeEnum ')

Aucun commentaire:

Enregistrer un commentaire