C++ 出现错误C2679的问题:二进制'+=';:未找到接受类型为';的右操作数的运算符;int';

C++ 出现错误C2679的问题:二进制'+=';:未找到接受类型为';的右操作数的运算符;int';,c++,class,compiler-errors,overloading,operator-keyword,C++,Class,Compiler Errors,Overloading,Operator Keyword,我正在为学校编写一段代码,定义一个具有各种功能的时间类,如添加和设置时间。我现在遇到的问题是重载我的“+=”运算符 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.

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

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

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";
时间c(61);
c+=120;

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

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

显然,您的
main
无法看到这些额外的
+
运算符,并且不知道它们的存在。你在哪里申报?这是不可能从你张贴的点点滴滴看出的。只是张贴了完整的代码。我将+运算符作为朋友运算符,将+=作为成员运算符。但在您的代码中,您从未定义过在右侧接受
int
+=
运算符。难怪编译器找不到它。要么定义它,要么停止尝试使用它。编译器会在错误消息中告诉您错误所在:
运算符+=
需要
时间&
,但您给它一个
int
,并且没有转换。我的印象是成员运算符只能接受一个参数。因为我试图将更新时间设置为c,所以我使用Time&作为参数。我不确定我应该把int放在操作符的什么地方。对这个话题真的很陌生。谢谢大家。最好让
operator+=
包含相关逻辑,然后让
operator+
调用
operator+=
。然后避免与
operator+
关联的所有临时对象。