Overloaded postfix operator

Write your question here.
I'm trying to have the overloaded postfix operator make day = 1 whenever its day 365 the first snipped is me creating the prototype, and the second snippet is the actual code definition, i can't seem to figure this out. Any help would be appreciated.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class DayOfYear
{
      
      
            
      public:
      DayOfYear(int d);
      void print();
      static string month;
      DayOfYear();
      DayOfYear(string m, int d);
      DayOfYear operator++(int);
      DayOfYear operator--();
      int day;
    
      
          
};


DayOfYear DayOfYear::operator++(int)
{
    
    DayOfYear temp = *this;    
    if(day = 365)
    {day = 1;}
    else
    {day ++;}
    return temp;
}
if(day = 365)

did you mean if(day == 365) ?
nah I get the same result. Thanks anyway though.
Are you absolutely sure you didn't mean if(day == 365) ? Because if(day = 365) sets day equal to 365, and always evaluates to true. Looking at your code, I'm pretty sure that's not what you intended.

Can you tell us what problem it is that you're actually seeing? We can't help you if you don't tell us what it is you need help with.
Last edited on
basically the program asks the user what day of the year do they want the date for.

so the day could be 364 which would be December 30.

I want the ++ operator to bring the day back to day 1 whenever I use it to increment objects that's day is 365. If the object isn't 365 I just want it to increment by 1 day

whenever I change it to if(day == 365)
it does the same thing, it still increments by one, but I end up with 366, not 1.

I appreciate the help.
Last edited on
Have you tried stepping through it with a debugger to see what's happening?
I've never used one before, but its worth giving it a try
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <cassert>

struct day_of_year
{
    day_of_year( int d = 1 ) : day(d) {}

    operator int() const { return day ; }

    static constexpr int MAX = 365 ;

    day_of_year& operator++ () { ++day ; if( day > MAX ) day = 1 ; return *this ; }
    day_of_year operator++ (int) { day_of_year temp = *this ; ++*this ; return temp ; }

    int day ;
};

int main()
{
    day_of_year d = 350 ;
    while(  d > 1 )  std::cout << d++ << '\n' ;
    assert( d == 1 ) ;
}

http://ideone.com/vBuGQk
Wow thanks
Topic archived. No new replies allowed.