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

Jul 13, 2013 at 6:55am
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";
     
}
Jul 13, 2013 at 6:56am
I'm using window 8
Jul 13, 2013 at 8:43am
i don't think that enumeration can be use for increment operations... maybe you better use switch
Jul 13, 2013 at 9:30am
chipp i need to use enumeration. you got any other way to solve it?
Jul 13, 2013 at 9:35am
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 Jul 14, 2013 at 8:29am
Jul 13, 2013 at 10:09am
can you give me more details JLBorges?

1
2
for( day_count=mon; day_count<=sun; /*day_count++*/ day_count = days(day_count+1) ) 
{ /* ... */ }
Jul 13, 2013 at 11:15am
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 Jul 13, 2013 at 11:21am
Jul 13, 2013 at 2:09pm
can't. anyone help please? easy way
Jul 13, 2013 at 2:45pm

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 Jul 13, 2013 at 2:45pm
Jul 13, 2013 at 2:49pm
> 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
Jul 13, 2013 at 3:25pm
thanks i will try. thanks so much JLBorges
Jul 13, 2013 at 3:28pm
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) ) 
{ /* ... */ }
Jul 13, 2013 at 10:35pm
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 Jul 13, 2013 at 10:36pm
Jul 14, 2013 at 7:23am
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?
Jul 14, 2013 at 8:24am
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 Jul 14, 2013 at 8:29am
Jul 14, 2013 at 9:29am
you're welcome, thx for the link... checking it now...
Topic archived. No new replies allowed.