Increments with overloaded operators

I have a class called Date that accepts three integers: day, month, and year, which are validated (no months with more than 31 days, Feb can have 29 days on a leap year, etc) and I have to add on to it to make it so it can increment and decrement the a given date, and also subtract one date from another using several overloaded operators.

Right now I'm working to overload my ++ prefix operator and making a Date class object called simplify that will make sure the dates increment to the correct month (make sure Jan 31, 2012 becomes Feb 1,2012 when incremented, and not Jan 32), but I'm having a couple problems. First, I keep getting errors when trying to overload my operator

Date.h
Date& operator ++ ();

Date.cpp
1
2
3
4
5
6
7
Date Date::operator - ()
{
	++day;
	simplify();
	return *this;

}


I keep getting errors telling me day and simplify are undefined. Can anyone tell me what I'm doing wrong?

Also, I'm having trouble with my simplify object. I've been working to make it handle every date exception, but I'm not sure about it because I'm working with "abs" and I've been told "abs" would be useful in this situation, but I'm not really familiar with it, and I'm not sure exactly what it does.

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
void Date::simplify()
{
	//Incrementors
	if (month = 4 || 6 || 9 || 11 && day > 30)
	{
		month += (day / 30);
		day = (day % 30);
	}
	else if ( month = 1 || 3 || 5 || 7 || 8 || 10 && day > 31)
	{
		month += (day / 31);
		day = day % 31;
	}
	else if ( month = 2 && day > 28 && year % 400 =! 0)
	{
		month += (day / 28);
		day = day % 28;
	}
	else if (month = 2 && day > 29 && year % 400 = 0)
	{
		month += (day / 29);
			day = day % 29;
	}
	else if (month = 12 && day > 31)
	{
		month = (day / 31);
		day = day % 31;
		year = year + 1;
	}
	//Decrementors
	else if (month = 4 || 6 || 9 || 11 && day < 1)
	{
		month -= ((abs(day) / 30) + 1);
		day = day - (abs(day) % 30);
	}
	else if (month = 3 || 5 || 7 || 8 || 10 || 12 && day < 1)
	{
		month -= ((abs(day) / 31) +1);
		day = 31 - (abs(day) % 31);
	}
	else if (month = 2 && day < 1 && year % 400 != 0)
	{
		month -= ((abs(day) / 28) +1);
		day = 28 - (abs(day) % 28);
	}
	else if (month = 2 && day < 1 && year % 400 = 0)
	{
		month -= ((abs(day) / 29 ) +1);
		day = 29 - (abs(day) % 29);
	}
	else if (month = 1 && day < 1)
	{
		month -= ((abs(day) / 31) + 1);
		day = 31 - (abs(day) % 31);
		year = year - 1;
	}

}


I know this is a bear of a post, but if someone could give me a little advice and point me in the right direction, I would greatly appreciate it. Thanks a lot.
You're trying to overload ++ right ?
Shouldnt Date Date::operator - () be Date Date::operator++ ()? (Ive never overloaded that operator - just curious)
Abs is absolute value

EDIT: Actually i think in your header file it should be Date& operator++ () and the implementation should be
1
2
3
4
5
6
7
 Date& Date::operator++();
{
	++day;
	simplify();
	return *this;

}
Last edited on
Whoops! Yes, you're right. I copy/pasted the wrong implementation. I'll give this a try, though.

EDIT: I fixed the header file, and the implementation is now error free! Now I just need to fix what is wrong with the arguments in my simplify object, and I'll be back on track.
Last edited on
Also, Visual Studio keeps informing me of errors in my simplify object, specifically for some of my arguments for month in my if statements. It keeps reading out to me that the expression needs to be a modifiable value, and I'm not sure what's wrong with the value I have in them.
Oh sorry i should of caught this earlyer but you need to replace = with == in all your if statements
= asigns a value while == compares values
You are using || incorrectly. || takes two arguments and returns false if both arguments is false, otherwise it returns true.

If an integer is used where a bool is expected 0 is considered to be false and everything else to be true.

If you do month == 4 || 6 the second argument is 6, which is true so that means the whole expression will always return true regardless of the value of the first argument. You will have to write it as month == 4 || month == 6 instead.

&& has higher precedence than || so && will bind closer than ||, meaning a || b && c will be treated as a || (b && c). In simplify() that is not what you want so you have to use parentheses to make the binding correctly.

To show what I mean,
if (month = 4 || 6 || 9 || 11 && day > 30)
has to be written as
if ((month == 4 || month == 6 || month == 9 || month == 11) && day > 30).
Thank you, Peter and Angel. I should have remembered that, but I'm getting back into learning C++ after a few months of not doing anything with it, so some of these things are slipping my mind.
By the way, you've only defined the pre-increment version of operator++. To support post increment you need the alternative form:

1
2
3
4
5
6
7
Date Date::operator++(int) // dummy int param
{
	Date aCopy(*this);  // assumes copy constructor available
	++day;
	simplify();
	return aCopy;
}


Notes

1 - The int param is a dummy, just to let the compiler know you mean post increment rather than pre-increment.
2 - This version of the operator returns a copy of the current instance by value
3 - This is why C++ programmers should get into the habit of using pre-increment rather than post-increment (to avoid copying)

Andy
Last edited on
All right, I've got my overloaded operators underway, and my simplify() object is almost back on track. I've turned all my = operators into ==, but I'm still having a problem in line 14
else if ( month == 2 && day > 28 && year % 400 =! 0)
It's now giving me an error message saying that month needs to be a modifiable value.

Every other line seems to be working fine, except this one that determines what is done if the month of February is not part of a leap year.
Last edited on
else if ( month == 2 && day > 28 && year % 400 =! 0)

The operator is !=, not =!

=! is being interpretted as
1
2
3
year % 400 =! 0
year % 400 = !0
year % 400 = 1  // <-nonsense 
Whoops again. Missed that one. Thanks, Disch.
Topic archived. No new replies allowed.