invalid operands of types - c++

青春壹個敷衍的年華 提交于 2021-02-10 07:57:27

问题


I have a class named ThreeDigits on c++ code. I overloaded the + operand, this way:

ThreeDigits* ThreeDigits::operator+(const ThreeDigits &number) const

{
   double result= getNumber()+number.getNumber();
   ThreeDigits* new_result=new ThreeDigits(result);
   return new_result;
}

but when I write on the main function:

    ThreeDigits* first=new ThreeDigits(2.55998);
    ThreeDigits* second=new ThreeDigits(5.666542);
    ThreeDigits* result=first+second;

I get the following compilation error: invalid operands of types ThreeDigits* and ThreeDigits* to binary operator+

Can you tell me what is the problem? thanks


回答1:


You are trying to sum pointers to objects instead of the objects themselves. To invoke the overloaded operator you must call it on objects, thus dereferencing the pointers.

By the way, creating all those objects with new a terrible way to do C++; in C++, unlike Java/C#, you should use new only when you have to, and allocate all the rest on the stack. Having the operator+ return a pointer to a newly created object is an abomination.

The C++ way of writing your code would be:

ThreeDigits ThreeDigits::operator+(const ThreeDigits &number) const
{
   return ThreeDigits(getNumber()+number.getNumber()); // construct a temporary and return it
}

// ...

ThreeDigits first(2.55998);
ThreeDigits second(5.666542);
ThreeDigits result=first+second;

By the way, the usual way of overloading arithmetic operators is first overloading the assigning versions (+=, -=, ...) and then build the "normal" version over them. For more info about operator overloading see the operator overloading FAQ.




回答2:


To use your operator as written, you'd need to write: ThreeDigits* result=*first+*second; in order to dereference the pointers.

However, your operator has at least two problems: One, it violates the principle of least surprise in that canonically operator+ returns by value. Second, it returns a pointer to new memory that will most likely be leaked in a wide variety of cases unless care is taken.

It looks like you may come from a Java background where everything is reference counted. Instead, the idiomatic C++ implementation would look like this:

ThreeDigits ThreeDigits::operator+(const ThreeDigits &number) const
{
   double result= getNumber()+number.getNumber();
   return ThreeDigits(result);
}

And used:

ThreeDigits first(2.55998);
ThreeDigits second(5.666542);
ThreeDigits result = first+second;



回答3:


You are not dereferencing the pointers. Try this:

ThreeDigits* result = *first + *second;



回答4:


Your operator accept two ThreeDigits references and returns a ThreeDigits pointer.

To use your operator as written, you must dereference first and second:

ThreeDigits* result = *first + *second;

This operator has an unusual signature though. operator+ should return a ThreeDigits (i.e. not a pointer to one). It's extremely bad practice for your operator to implicitly new (or malloc) data because users will not expect this. RVO means that returning a copy isn't such a big deal anyway.



来源:https://stackoverflow.com/questions/7405973/invalid-operands-of-types-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!