c++ - 出现错误 C2679 : binary '+=' : no operator found which takes a right-hand operand of type 'int' 的问题

标签 c++ class compiler-errors overloading operator-keyword

我正在为学校编写一段代码,定义一个具有各种功能的 Time 类,例如添加和设置时间。我现在遇到的问题是重载我的“+=”运算符。

Time& Time::operator+=(Time& a) {
    *this = a + *this;
    return *this;
}

我已经重载了“+”运算符,所以我只是为我的“+=”运算符调用它。
 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;
    }

在我的 int main() 中,我正在使用:
Time c(61);
c += 120;
cout << "\tc += 120;\t\t";
cout << "c = " << c << "\n";

这给了我错误。我不确定我做错了什么,因为我已经用 int 和 double 参数重载了“+”运算符。

可以做些什么来解决这个问题?

这是完整的类定义和声明。我正在创建类并针对我的教授提供的驱动程序运行它,因此 int main() 无法更改。
#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;

}

最佳答案

解决了这个问题,感谢评论员。我没有在 += 运算符中使用 Time& 参数,而是将其更改为要添加的任何类型。

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;
}

关于c++ - 出现错误 C2679 : binary '+=' : no operator found which takes a right-hand operand of type 'int' 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41949910/

相关文章:

c++ - C++ 函数中对默认第一个参数的支持历史

javascript - 使用动态扩展类表达式创建新元素

c# - 将两个不同的字符串列表合并到一个类列表中

使用 system() 使用 gcc 进行编译

c++ - 如何使用合并排序计算大量输入的反转次数

c++ - stdlib.h 中的 rand 有多糟糕?

c++ - 如何将垫子中包含的图像复制到更大的垫子上?

ios - 对类对象使用 objc_setAssociatedObject 是否正确?

javascript - 代码可在codeacademy的站点上运行,但不能在coderbyte上运行

swift - 无法编译简单的 Swift #selector