Search code examples
c++gccenumsoperator-overloadingavr

"no 'operator++(int)' declared for postfix '++' [-fpermissive]" on enums


I have got the enum

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

} CurrentProgram;

Now, I am trying to increment CurrentProgram like this: CurrentProgram++, but the compiler complains: no 'operator++(int)' declared for postfix '++' [-fpermissive]. I think there IS such an operator that increments "enums", but if there is not, how do I get the successor of one of these values?


Solution

  • There is no such an operator for enumerations. But you can write that operator yourself. For example

    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 );
    }