0

enum Days { Sunday, Monday, Tuesday, Wednesday,Thursday, Friday, Saturday };

for( Days d = Sunday; d < Saturday; ++d )
cout << "d is: " << d << endl;

C++ won't know how to increment d unless we define how that operator works for the type Days. Overload operator ++ so this "for" loop compiles and runs correctly.

day &operator++(day &d) {
d = (day)(d + 1);
return d;
}

i wrote this is this correct

2
Contributors
3
Replies
4
Views
8 Years
Discussion Span
Last Post by Ancient Dragon
0

i really tired but cant compile it never complied it without a class so i am confused . i have done overloading using class but this one i have no clue !

0

this works

enum Days { Sunday, Monday, Tuesday, Wednesday,Thursday, Friday, Saturday };

void operator++(Days& d)
{
    if( d == Saturday )
        d = Sunday;
    else
        d = (Days)((int)d + 1);
}

int main()
{
    Days d = Sunday;
    do
    {
        switch(d)
        {
        case Sunday: cout << "Sunday\n"; break;
        case Monday: cout << "Monday\n"; break;
        case Tuesday: cout << "Tuesday\n"; break;
        case Saturday: cout << "Saturday\n"; break;
        default: cout << (int)d << "\n"; break;
        }
        ++d;
    }while( d != Sunday );
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.