Problem overloading pre-increment operator (++)

Hi
I'm not sure if this is a beginner's question...
I'm trying to create a Date class and override some operators.
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
class Date{
private:
	int _day, _month, _year;
....
public:
        Date& operator ++();
	Date operator ++(int);
.....
};
/*
 * Overloads the ++ postfix operator.
 * Increments the day by 1.
 */
Date Date::operator ++(int u){
	Date tmp=Date(*this);
	_day++;
	correct();
	return tmp;
}

/*
 * Overloads the ++ prefix operator.
 * Increments the day by 1.
 */
Date& Date::operator ++(){
	++_day;
	correct();
	return *this;
}

int main()
{
	int d1,m1,y1,d2,m2,y2;
	cin>>d1>>m1>>y1>>d2>>m2>>y2;
	Date first=Date(d1,m1,y1);
	Date second=Date(d2,m2,y2);
	first++.print();
	++first.print();
	second.print();
	return 0;
}


The third to last line in main() (line 38) throws an error:
 
error: no pre-increment operator for type


Just a side note, the correct() method simply fixes the date so that the day/month/year is correct (not too many days in a month, months in a year...).
Also you can assume that the copy constructor works properly (I tested it).
I'm not sure if it's relevant, but I'm using GCC version 4.4.3
Last edited on
Shouldn't it be Date operator ++(&Date);?
Last edited on
Ah, you're right. Tried wrapping ++first in paranthesis? I think ++ has lower precedence than .
Last edited on
That worked :D
Now why didn't I think about that...?
Thanks.
Topic archived. No new replies allowed.