operator overloading an enum

hi guys just wondering if it's possible to overload operators for enums?

the code actually compiles fine so it lets me overload the ++ operator BUT in main when I try to use it m++ it gives me an error no operator++(int) declared for postfix '++'

thanks

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
 #include <iostream>

using namespace std;

enum Month{

   JAN = 1,
   FEB,
   MARCH,
   APRIL,
   MAY,
   JUNE,
   JULY,
   AUGUST,
   SEPTEMBER,
   OCTOBER,
   NOVEMBER,
   DECEMBER
};

Month operator++(Month& m){

   if(m == DECEMBER){

      m = JAN;
      return m;
   }else{

     m = Month(m+1);
     return m;
   }
}

int main()
{

  Month m = FEB;
  cout << m << endl;
  m++;

}
Last edited on
You have overloaded the prefix increment operator (++m).

To overload the postfix increment operator (m++) you need add a dummy int parameter.

 
Month operator++(Month& m, int){
thanks Peter,I thought maybe it wasn't possible to overload enums,

Topic archived. No new replies allowed.