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++;
}
{
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 ==========
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)
}
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
The above will not help. It just adds yet another error.
A possible fix would have been this declaration: intTime :: operator + (Time a)
... so that the declared return type matches the value returned by the function.
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