C ++ operator overload in enum class

So, I am writing a game using C ++ and in my training state, I have different steps that the user views, explaining how the game works. I want to increase the step at which the user is logged in after a specific action. (Mouse click). I tried to reload the statement ++, but I got an error message binary '++': 'STEPS' does not define this operator or a conversion to a type acceptable to the predefined operator. I am using visual studio and error code C2676.

I have an enum class configured as follows:

enum class STEPS
{
    ONE,
    TWO,
    END_OF_LIST
};

STEPS& operator++(STEPS& s)
{
    s = staic_cast<STEPS>(static_cast<int>(s) + 1);
    if (s == STEPS::END_OF_LIST)
    {
        s = static_cast<STEPS>(static_cast<int>(s) - 1);
    }
    return s;
}

In my update function of my textbook status class, I check if the mouse has been clicked. If it's me, I'm trying to increase the steps.

// this is defined in the header and STEPS :: ONE is set for initialization STEPS steps;

TutorialState::Update()
{
    // If mouse was clicked
    if (mouse.Left())
    {
        steps++; // this is giving me an error.
    }
}
+4
source share
1
STEPS& operator++(STEPS& s);

++step.

step++,

STEPS operator++(STEPS& s, int) { auto res = s; ++s; return res; }

int pre post increment.

http://en.cppreference.com/w/cpp/language/operator_incdec .

+13

Source: https://habr.com/ru/post/1686052/


All Articles