简体   繁体   English

为什么类型转换不适用于 += 运算符?

[英]Why type conversion does not work with += operator?

I wrote the following code as a practice for my OOP exam:我编写了以下代码作为我的 OOP 考试的练习:

#include <iostream>
using namespace std;

class doble
{
public:
    doble(double D):d(D){}
    inline operator double(){return d;}
private:
    double d;
};

int main()
{
    doble a = 1.5, b = 10.5;
    doble c = 5.25, d = c;
    cout << c / d * b + b * c - c * c / b + b / c << endl; //65

    d = a = b += c;
    cout << d <<" "<< a <<" "<< b <<" "<< c << endl; //15,75 15,75 15,75 5,25
}

I get an error in the line with the operator '+=' that says: 'no operator "+=" matches these operands'.我在带有运算符“+=”的行中收到错误消息:“没有运算符”+=“匹配这些操作数”。 I don't know what else can I do because the overloading of arithmetic operators, assignment, insertion, or the use of friendly functions is not allowed in this exercise.我不知道我还能做什么,因为在这个练习中不允许重载算术运算符、赋值、插入或使用友好的函数。

Thank you!谢谢!

I thought that with the conversion to double, the operator '+=' will work with doble.我认为转换为 double 后,运算符“+=”将与 doble 一起使用。

Your example works if you defined the operator += properly.如果您正确定义了运算符+= ,则您的示例有效。

    doble& operator += ( const doble& rhs ) {
        d += rhs.d;
        return *this;
    }

Produces产品

Program stdout
65
15.75 15.75 15.75 5.25

Godbolt: https://godbolt.org/z/rnsMf7aYh Godbolt: https://godbolt.org/z/rnsMf7aYh

I thought that with the conversion to double, the operator '+=' will work with doble.我认为转换为 double 后,运算符“+=”将与 doble 一起使用。

The problem is that both of the operands b and c are of the same type so there is no type conversion to double required here.问题是操作数bc都是同一类型,因此这里不需要类型转换为double Moreover, since you've not overloaded operator+= for doble , it produces the mentioned error.此外,由于您没有为doble重载operator+= ,它会产生上述错误。


I don't know what else can I do我不知道我还能做什么

To solve this you can overload operator+= :解决这个问题,您可以重载operator+=

#include <iostream>


class doble
{
public:
    doble(double D):d(D){}
    inline operator double(){return d;}
    //overload operator+=
     doble& operator+=(const doble& rhs) 
    {                           
        /* addition of rhs to *this takes place here */
        d+=rhs.d;
        return *this; // return the result by reference
    }
private:
    double d;
};

int main()
{
    doble b = 10.5;
    doble c = 5.25;
    b += c;
    std::cout << b << c;
}

Demo演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么输出带转换运算符的类不适用于std :: string? - Why does outputting a class with a conversion operator not work for std::string? 为什么隐式类型转换在模板推导中不起作用? - Why does the implicit type conversion not work in template deduction? 为什么这种二进制转换不起作用? - why this binary conversion does not work? 为什么这个显式转换运算符适用于g ++而不适用于Visual Studio 2013? - Why does this explicit conversion operator work with g++ but not Visual Studio 2013? 为什么g ++在带有转换运算符和不可访问的函数调用运算符的类型中失败了std :: function &lt;&gt;的init? - Why does g++ fail init of std::function<> from type with conversion operator and inaccessible function call operators? binary &gt; T 未定义此运算符或转换为预定义运算符可接受的类型 - binary > T does not define this operator or a conversion to a type acceptable to the predefined operator 模板化类型转换运算符= - Templated type conversion operator = 类类型的转换运算符? - Conversion operator for class type? operator== 的隐式类型转换 - Implicit type conversion for operator== 调用转换运算符不适用于静态const变量 - Call conversion operator does not work on with static const variable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM