What I am missing plz help?

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
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)
}
what is the correction sir then,
Thanks
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
}
Maybe something like this. (I've not tested this so it may not be correct):
1
2
3
4
5
6
7
8
9
10
11
Time Time :: operator + (Time a)
{
    Time temp;
    int TS = total_seconds() + a.total_seconds() ;

    temp.seconds = TS % 60;
    temp.minutes = TS / 60;
    temp.hours   = TS / (60 * 60);
    temp.days    = TS / (24 * 60 * 60);
    return temp;
}}

The above is a reasonable guideline, but some of the figures are wrong. Rather than fix that here, I offer an alternative suggestion:
1
2
3
4
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);
Last edited on
davetep wrote:
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:
int Time :: operator + (Time a)
... so that the declared return type matches the value returned by the function.
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
I think this is a bit better than my previous attempt...
1
2
3
4
5
6
7
8
9
10
11
12
13
Time Time::operator+(Time a)
{
    int TS = total_seconds() + a.total_seconds() ;

    Time temp;
    
    temp.seconds = TS % 60;
    temp.minutes = (TS / 60) % 60;
    temp.hours   = (TS / (60 * 60)  ) % 24;
    temp.days    = TS / (24 * 60 * 60);
    
    return temp;
}
Topic archived. No new replies allowed.