0

I want to write overloading operator, that increments enum type:

#include <iostream>
#include <vector>
#include <string>
#include <stdexcept>
using namespace std;


enum Type {grun,grun_blinkend,gelb,rot ,rot_gelb,gelb_blinkend}



Type& operator++(Type& color){
    return color = static_cast<Type>(++static_cast<int>(Type)); 
};

But it throws me an error:

error: expected initializer before '&' token

Why and how can I fix it?

I need this operator in order to iterate over the Type for "traffic light" simulation:

Ampel Ampel::weiter(){
    if(zustand ==  Type(rot_gelb)){
        zustand = Type(grun);
        return Ampel(zustand);
    }
    ++zustand;
    return Ampel(zustand);
}
vitsuk
  • 85
  • 7

1 Answers1

1

This is a solution, but you must be cautious about an integer higher to gelb_blinkend, for a good explanation about that I recommend see this question and also you should think about change enum for enum class, for this I suggest this another question.

enum Type {grun,grun_blinkend,gelb,rot ,rot_gelb,gelb_blinkend};

Type& operator++(Type& color){
    int current = static_cast<int>(color);
    return color = static_cast<Type>(++current); 
};
JTejedor
  • 1,082
  • 10
  • 24