【问题标题】:Issue with error C2679: binary '+=': no operator found which takes a right-hand operand of type 'int'错误 C2679 的问题:二进制“+=”:未找到采用“int”类型的右手操作数的运算符
【发布时间】:2017-01-31 04:56:45
【问题描述】:

我正在为学校编写一段代码,定义一个具有各种功能的 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;

}

【问题讨论】:

  • 好吧,显然你的main 看不到这些额外的+ 操作符并且不知道它们的存在。你在哪里声明它们?从你贴出来的点点滴滴是不可能弄清楚的。
  • 刚刚发布了完整的代码。我将 + 运算符作为朋友运算符,将 += 作为成员运算符。
  • 但是在您的代码中,您从未定义过在右侧采用 int+= 运算符。难怪编译器找不到它。要么定义它,要么停止尝试使用它。
  • 编译器会在其错误消息中告诉您有什么问题:operator+= 采用 Time&amp;,但您给它的是 int,但没有转换。
  • 我的印象是成员运算符只能带一个参数。由于我试图更新时间 c,因此我使用 Time& 作为参数。我不确定应该将 int 放在我的运算符中的哪个位置。对这个话题真的很陌生。谢谢大家。

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


【解决方案1】:

解决了这个问题,感谢评论员。我没有在 += 运算符中使用 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;
}

【讨论】:

  • 最好让operator+=包含相关逻辑,然后让operator+调用operator+=。然后你避免所有与operator+相关的临时对象。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多