Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 926
HerpDerpington Avatar asked Dec 24 '13 18:12

HerpDerpington


1 Answers

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 );
}
like image 176
Vlad from Moscow Avatar answered Sep 25 '22 15:09

Vlad from Moscow