I'm working on a piece of code for school, defining a Time class with various functions such as adding and setting time. The issue I have right now is overloading my "+=" operator.
Time& Time::operator+=(Time& a) {
*this = a + *this;
return *this;
}
I have overloaded the "+" operators already, so I am just calling on it for my "+=" operator.
Time operator+(Time a, Time b) {
int sumhr = a.hour + b.hour;
int summin = a.minute + b.minute;
if (summin > 59) {
sumhr += summin / 60;
summin = summin % 60;
}
if (sumhr > 23)
sumhr = sumhr % 24;
return Time(sumhr, summin);
}
Time operator+(Time a, int addminutes) {
Time t = a + Time(addminutes);
return t;
}
Time operator+(Time a, double addhours) {
Time t = a + Time(addhours);
return t;
}
In my int main(), I am using:
Time c(61);
c += 120;
cout << "\tc += 120;\t\t";
cout << "c = " << c << "\n";
which gives me the error. I'm not sure what I'm doing wrong, since I overloaded the "+" operator with int and double parameters already.
What can be done to solve this problem ?
Here is the full class definition and declaration. I'm working on creating the class and running it against a driver provided by my professor, so the int main() can't be changed.
#include <iostream>
using namespace std;
using std:: cout;
class Time {
private:
int hour;
int minute;
public:
Time();
Time(int min);
Time(int hr, int min);
Time(double hrs);
int minutes();
int hours();
Time& operator+=(Time& a);
void set_minutes(int min);
void set_minutes(double min);
void set_hours(int hr);
void set_hours(double hr);
friend ostream& operator<<(ostream& os, const Time& t);
friend Time operator+(Time a, Time b);
friend Time operator+(Time a, int addminutes);
friend Time operator+(Time a, double addhours);
friend bool operator<(Time a, Time b);
friend bool operator==(Time a, Time b);
friend bool operator>=(Time a, Time b);
friend bool operator!=(Time a, Time b);
};
Time::Time() {
hour = 0;
minute = 0;
}
Time::Time(int min) {
if (min > 59) {
hour = min / 60;
minute = min % 60;
}
else {
hour = 0;
minute = min;
}
}
Time::Time(int hr, int min) {
if (min > 59) {
hour = min / 60;
minute = min % 60;
}
else {
hour = hr;
minute = min;
}
if (hour > 23)
hour = hour % 24;
}
Time::Time(double hrs) {
double fraction = 0;
fraction = hrs - (int)hrs;
minute = fraction * 60;
hour = hrs - fraction;
if (hour > 23)
hour = hour % 24;
}
int Time::minutes() {
return minute;
}
int Time::hours() {
return hour;
}
ostream& operator<<(ostream& os, const Time& t)
{
if (t.minute > 9)
os << t.hour << ":" << t.minute;
else
os << t.hour << ":0" << t.minute;
return os;
}
Time operator+(Time a, Time b) {
int sumhr = a.hour + b.hour;
int summin = a.minute + b.minute;
if (summin > 59) {
sumhr += summin / 60;
summin = summin % 60;
}
if (sumhr > 23)
sumhr = sumhr % 24;
return Time(sumhr, summin);
}
Time operator+(Time a, int addminutes) {
Time t = a + Time(addminutes);
return t;
}
Time operator+(Time a, double addhours) {
Time t = a + Time(addhours);
return t;
}
Time& Time::operator+=(Time& a) {
*this = a + *this;
return *this;
}
bool operator<(Time a, Time b) {
if (a.hour > b.hour)
return false;
else if (a.hour < b.hour)
return true;
else {
if (a.minute > b.minute)
return false;
else if (a.minute == b.minute)
return false;
else
return true;
}
}
bool operator==(Time a, Time b) {
if (a.hour != b.hour)
return false;
else {
if (a.minute != b.minute)
return false;
else
return true;
}
}
bool operator>=(Time a, Time b) {
if (a < b)
return false;
else
return true;
}
bool operator!=(Time a, Time b) {
if (a == b)
return false;
else
return true;
}
void Time::set_minutes(int min) {
minute = min % 60;
if (minute == 60) {
minute = 0;
}
}
void Time::set_minutes(double min) {
minute = (int)min % 60;
double rounding = min = (int)min;
if (rounding >= 0.5) {
minute += 1;
if (minute == 60) {
minute = 0;
}
}
}
void Time::set_hours(int hr) {
hour = hr % 24;
if (hour == 24)
hr = 0;
}
void Time::set_hours(double hr) {
hour = (int)hr % 24;
if (hour == 24)
hr = 0;
}
int main() {
Time a;
Time b(5);
Time c(61);
Time d(47, 59);
Time X(5.0);
Time Y(1.5);
Time Z(25.1);
cout << "Testing constructors:\n";
cout << "\tTime a;\t\t\t";
cout << "a = " << a << "\n";
cout << "\tTime b(" << b.minutes() << ");\t\t";
cout << "b = " << b << "\n";
cout << "\tTime c(61);\t\t";
cout << "c = " << c << "\n";
cout << "\tTime d(47,59);\t\t";
cout << "d = " << d << "\n";
cout << "\tTime X(5.0);\t\t";
cout << "X = " << X << "\n";
cout << "\tTime Y(1.5);\t\t";
cout << "Y = " << Y << "\n";
cout << "\tTime Z(25.1);\t\t";
cout << "Z = " << Z << "\n";
cout << "Testing operator+:\n";
cout << "\te = b + c;\t\te = " << b + c << "\n";
cout << "\tf = d + 2;\t\tf = " << 2 + d << "\n";
cout << "\tg = c + 2.75;\t\tg = " << 2.75 + c << "\n";
cout << "Testing operator+=:\n";
c += 120;
cout << "\tc += 120;\t\t";
cout << "c = " << c << "\n";
c += 1.99166666;
cout << "\tc += 1.99166666;\t";
cout << "c = " << c << "\n";
c += 1.99166667;
cout << "\tc += 1.99166667;\t";
cout << "c = " << c << "\n";
cout << "Testing other member functions:\n";
c.set_minutes(60);
cout << "\tc.set_minutes(60);\t";
cout << "c = " << c << "\n";
c.set_minutes(123.45);
cout << "\tc.set_minutes(123.45);\t";
cout << "c = " << c << "\n";
c.set_minutes(67.89);
cout << "\tc.set_minutes(67.89);\t";
cout << "c = " << c << "\n";
cout << "\tc.set_hours(45);\t";
c.set_hours(45);
cout << "c = " << c << "\n";
cout << "\tc.set_hours(1.9);\t";
c.set_hours(1.9);
cout << "c = " << c << "\n";
cout << "\tc.set_hours(1.9999);\t";
c.set_hours(1.9999);
cout << "c = " << c << "\n";
cout << "Testing comparison operators:\n";
if (b < c)
cout << b << " occurs earlier in the day than " << c << ", ";
if (!(b == c))
cout << "hence b != c.\n";
if (c >= b)
cout << c << " occurs later in the day than " << b << ", ";
if (b != c)
cout << "hence c != b.\n";
return 0;
}
Solved this, thanks to the commentators. Instead of using a Time& parameter in my += operators, I change it to whatever type I am adding.
Time operator+(Time a, int addminutes) {
Time t = a + Time(addminutes);
return t;
}
Time operator+(Time a, double addhours) {
Time t = a + Time(addhours);
return t;
}
Time& Time::operator+=(int addMinutes) {
*this = addMinutes + *this;
return *this;
}
Time& Time::operator+=(double addhours) {
*this = addhours + *this;
return *this;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.