| flyer86 (4) | |
|
Hi guys My compiler is giving me the error please have a look to the program and Microsoft visual C++ compile error, "operator + " is giving some error when i return the result to main(). (I mentioned it in code with '====>' sign) #include <iostream> #include <conio.h> using namespace std; class Time { private: int days,hours,minutes,seconds; void adjust_time(); public: Time(int d, int h, int m, int s); //Time(int s); Time () {}; ~Time(){}; int total_seconds(); void output(); Time operator + (Time ); }; Time :: Time () { seconds=minutes=hours=days= 0; } Time::Time (int d, int h, int m, int s) { days= d; hours= h; minutes =m; seconds =s; adjust_time(); } void Time :: adjust_time() { while (seconds>=60) { seconds-=60; minutes++; } while (minutes>=60) { minutes-=60; hours++; } while (hours>=24) { hours-=24; days++; } } /*Time :: Time (int s) { seconds= s; minutes=hours=days= 0; adjust_time(); }*/ int Time :: total_seconds() { return(seconds+(minutes*60)+(hours*60*60)+(days*24*60*60) ); } Time Time :: operator + (Time a) { int TS; TS = total_seconds() + a.total_seconds() ; return (TS); =====> (this line is giving some error) } void Time :: output() { cout << days << " days " << hours << " hours " << minutes << " minutes " << seconds << " seconds "; } int main () { Time t1(0,10,10,10),t2(0,10,10,50); Time t3(0,10,10,10); Time t4; t4 = t1 + t2 + t3; cout << "Time Addition" << endl; t4.output(); _getch(); return 0; }; ERROR: 1>------ Build started: Project: Time class, Configuration: Debug Win32 ------ 1> time class.cpp 1>c:\documents and settings\my documents\visual studio 2010\projects\time class\time class\time class.cpp(72): error C2664: 'Time::Time(const Time &)' : cannot convert parameter 1 from 'int' to 'const Time &' 1> Reason: cannot convert from 'int' to 'const Time' 1> No constructor could take the source type, or constructor overload resolution was ambiguous ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== | |
|
Last edited on
|
|
| vlad from moscow (3112) | |
|
The return type of this overload operator is Time. However your return an expression of type int. The class has no a constructor with argument of type int. Time Time :: operator + (Time a) { int TS; TS = total_seconds() + a.total_seconds() ; return (TS); =====> (this line is giving some error) } | |
|
|
|
| flyer86 (4) | |
|
what is the correction sir then, Thanks | |
|
|
|
| davetep (4) | |
|
two possible corrections. Either: 1) change the declaration: Time Time :: operator + (int x) <===== put an int here, because you returned an int at the end of the function or 2) change the code so that the last line returns a Time object to match the declaration, like so: Time Time :: operator + (Time a) { Time t; int TS; TS = total_seconds() + a.total_seconds(); t.seconds = TS; return t; <======you are now returning an object } | |
|
|
|
| Chervil (812) | |||||
Maybe something like this. (I've not tested this so it may not be correct):
The above is a reasonable guideline, but some of the figures are wrong. Rather than fix that here, I offer an alternative suggestion:
Time::Time(int s);
| |||||
|
Last edited on
|
|||||
| Chervil (812) | |||
The above will not help. It just adds yet another error. A possible fix would have been this declaration: int Time :: operator + (Time a)... so that the declared return type matches the value returned by the function. | |||
|
|
|||
| flyer86 (4) | |
|
thanks all fellows for constructive guidance. @Chervil Time Time :: operator + (Time a) { return Time(total_seconds() + a.total_seconds()); } This makes use of the (currently commented out) constructor, Time::Time(int s); actually i want to do it without this Time (int s) constructor let me try your int Time :: operator + (Time a) thank you | |
|
|
|
| Chervil (812) | |||
I think this is a bit better than my previous attempt...
| |||
|
|
|||