枚举类中的c ++运算符重载

Vin*_*nce 4 c++ enums overloading class operator-keyword

所以我正在使用c ++编写游戏,在我的教程状态下,我有不同的步骤,用户经历,解释游戏的工作原理.我想在执行某个操作后增加用户所在的步骤.(鼠标单击).我试过重载++运算符,但我得到一个错误说binary '++': 'STEPS' does not define this operator or a conversion to a type acceptable to the predefined operator.我正在使用visual studio,它的错误代码C2676.

我的枚举类设置如下:

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;
}
Run Code Online (Sandbox Code Playgroud)

在我的教程状态类的更新功能中,我检查是否单击了鼠标.如果是我正试图增加步骤.

//这是在标题中定义的,并在初始化时设置为STEPS :: ONE STEPS steps;

TutorialState::Update()
{
    // If mouse was clicked
    if (mouse.Left())
    {
        steps++; // this is giving me an error.
    }
}
Run Code Online (Sandbox Code Playgroud)

Jar*_*d42 13

STEPS& operator++(STEPS& s);
Run Code Online (Sandbox Code Playgroud)

是为了++step.

因为step++,你需要

STEPS operator++(STEPS& s, int) { auto res = s; ++s; return res; }
Run Code Online (Sandbox Code Playgroud)

已选择使用额外参数int来区分前后增量运算符.

您可以阅读http://en.cppreference.com/w/cpp/language/operator_incdec了解更多详情.