I don't know what the problem. Can't detect the error.

Help me solve this error please

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;
enum days {mon=1, tue, wed, thu, fri, sat, sun};

void main()
{
     
     enum days day_count;
     
     cout<<"Simple day count\n";
     cout<<"       using enum\n";
     
     for(day_count=mon; day_count<=sun; day_count++)
     cout<<" "<<day_count<<"\n";
     
}
I'm using window 8
i don't think that enumeration can be use for increment operations... maybe you better use switch
chipp i need to use enumeration. you got any other way to solve it?
Either
1
2
for( day_count=mon; day_count<=sun; /*day_count++*/ day_count = days(day_count+1) ) 
{ /* ... */ }


Or overload the increment operator:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>

enum day_t { mon=1, tue, wed, thu, fri, sat, sun, invalid } ;

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

// EDIT: corrected to fix the error pointed out by chipp
day_t operator++( day_t& d, int ) { auto old_value = d ; ++d ; return old_value ; }

int main()
{
     for( day_t d = mon ; d != invalid ; ++d ) std::cout << d << ' ' ;
     std::cout << '\n' ;
}
Last edited on
can you give me more details JLBorges?

1
2
for( day_count=mon; day_count<=sun; /*day_count++*/ day_count = days(day_count+1) ) 
{ /* ... */ }
1
2
3
4
5
day_t & operator ++( day_t &d ) 
{
   if ( d ==  sun ) return d = mon;
   return d = static_cast<day_t>( d + 1 ) ; 
}

Last edited on
can't. anyone help please? easy way

I do not understand what help you need. All was already shown. What is the problem? You may simplify your ptogram the following way

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>

using namespace std;
enum days {mon=1, tue, wed, thu, fri, sat, sun};

int main()
{
     cout<<"Simple day count\n";
     cout<<"       using enum\n";
     
     for ( int day_count = mon;  day_count <= sun; day_count++ )
     {
          cout << " " << day_count << "\n";
     }
}
Last edited on
> can you give me more details JLBorges?
> for( day_count=mon; day_count<=sun; /*day_count++*/ day_count = days(day_count+1) )
>> easy way

There isn't anything easier than:
1
2
for( day_count=mon; day_count<=sun; day_count = days(day_count+1) ) 
{ /* ... */ }


1. day_count can be implicitly converted to an int
2. day_count+1 yields an integer with a value one higher than the integer value of day_count
3. days(day_count+1) casts that integer to an object of type days
4. which is assigned to day_count
thanks i will try. thanks so much JLBorges
JLBorges, what do you mean by this?
i'm not very understand
1
2
for( day_count=mon; day_count<=sun; /*day_count++*/ day_count = days(day_count+1) ) 
{ /* ... */ }
Expression day_count+1 is implicitly converted to an integral type. However an integral expression can not be implicitly converted to an enumeration type. You shall explicitly specify the type of enumeration to which you want to convert an integral expression. And this record

days(day_count+1)

means that expression

day_count+1

must be converted to the enumeration type enum days.
Last edited on
1
2
3
day_t& operator++( day_t& d ) { return d = day_t( d + 1 ) ; }

day_t operator++( day_t& d, int ) { return ++d ; }


actually i asked this code in another group, they said that the postfix operator overloading is wrong, it should be:

1
2
3
4
5
day_t operator++( day_t& d, int ) {
	day_t dummy = d;
	++d;
	return dummy;
}


it wasn't my actual question, but this one is right too... so i just want to verify your code...

and, can someone give me links for operator++ overloading?
Yes, this is wrong:
day_t operator++( day_t& d, int ) { return ++d ; }

Should be as in your example; make a copy before the increment and return the copy.

Thanks for pointing it out, I've corrected it now.

operator++ overloading:
http://www.parashift.com/c++-faq-lite/increment-pre-post-overloading.html
Last edited on
you're welcome, thx for the link... checking it now...
Topic archived. No new replies allowed.