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

>>i wrote this is this correct
Don't know -- did you test it?

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 !

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 );
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.