Consider the following code:
#include<iostream>
enum week
{
sun=1,
mon,
tue,
wed,
thr,
fri,
sat
};
week &operator++(week& day,int)
{
if(day==sat)
day=sun;
else
day++; // This expression
return day;
}
int main()
{
week day=sun;
for(int i=0;i<=10;i++,day++)
{
std::cout<<day;
}
}
In the expression day++ it goes into infinite recursion.
If I cast it like ((int)day)++ the compiler gives the following error:
error: lvalue required as increment operand
If I change the line to day=week(((int)day)+1) it works. But how to fix the above code so it works with the ++ operator?
++operator. What's wrong withday=week(((int)day)+1);? You can also write simplyreturn day = week(((int)day%7)+1);.++as much.week next(week day) { if (day==sat) return sun; return week(((int)day)+1);and in the for loop, useday = next(day).