Overloaded postfix operator

Jul 18, 2013 at 2:06pm
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;
}
Jul 18, 2013 at 2:12pm
if(day = 365)

did you mean if(day == 365) ?
Jul 18, 2013 at 2:21pm
nah I get the same result. Thanks anyway though.
Jul 18, 2013 at 2:36pm
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 Jul 18, 2013 at 2:37pm
Jul 18, 2013 at 2:58pm
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 Jul 18, 2013 at 2:59pm
Jul 18, 2013 at 3:17pm
Have you tried stepping through it with a debugger to see what's happening?
Jul 18, 2013 at 3:20pm
I've never used one before, but its worth giving it a try
Jul 18, 2013 at 3:32pm
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
Jul 18, 2013 at 3:52pm
Wow thanks
Topic archived. No new replies allowed.