Friday, September 16, 2011

chapter11

ERROR:default argument given for parameter...
You should know that you could only make default value for parameter in the definition not on the implementation. Just take it out from your cpp file, only have it on header file.

ERROR:assignment of data-member ‘Time::hours’ in read-only structure
Time sum(Time& time) const
{
time->hours = time.hours + time->hours;
...
}
显然这里出错是因为sum function是const的,试图改变这个this,不对
可改为
Time sum(Time& time) const
{
Time sum;
sum.hours = XXX;
sum.minutes =XXX;
return sum;
}

ERROR: redefinition of Time() function
constructor could be initialized in the header file, then you don't need to have it in the cpp implementation file.

ERROR: no match for operator+
note that there should be a space between operator and op, like: operator +
Time operator +(const Time& time) const;

ERROR: no match for operator *
Remember, the left operand is the invoking object. If you overload * in class Time, you need to make Time object on the left side, like this:
time1 = time1 * 1.5; because it could be translated into: time1 = time1.operator *(1.5);
not: time1 = 1.5 * time1; it cannot correspond to a member function, 1.5.operator *(Time &time)????
Could use non-member function!@!!

Question1:
I made ----- Time sum(const Time& time) const ---, how come I still can use time1=time1+time? It has changed time1. but aren't we supposed to make time1 const, that is the second const for, right?

No comments:

Post a Comment