"没有'运算符++(int)'在枚举上声明为后缀'++'[-fpermissive]"

Her*_*ton 5 c++ enums gcc avr operator-overloading

我得到了枚举

enum ProgramID
{
    A = 0,
    B = 1,
    C = 2,
    MIN_PROGRAM_ID = A,
    MAX_PROGRAM_ID = C,

} CurrentProgram;
Run Code Online (Sandbox Code Playgroud)

现在,我试图CurrentProgram像这样增加:CurrentProgram++,但编译器抱怨:no 'operator++(int)' declared for postfix '++' [-fpermissive].我认为有这样一个运算符增加"枚举",但如果没有,我如何获得其中一个值的后继?

Vla*_*cow 5

枚举没有这样的运算符.但是你可以自己编写那个操作符.例如

ProgramID operator ++( ProgramID &id, int )
{
   ProgramID currentID = id;

   if ( MAX_PROGRAM_ID < id + 1 ) id = MIN_PROGRAM_ID;
   else id = static_cast<ProgramID>( id + 1 );

   return ( currentID );
}
Run Code Online (Sandbox Code Playgroud)