类型的无效操作数 - c++

发布于 2024-12-04 12:40:05 字数 556 浏览 2 评论 0原文

我有一个名为 ThreeDigits 的 C++ 代码类。我这样重载了 + 操作数:

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

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

但是当我在 main 函数上写入时:

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

我收到以下编译错误: ThreeDigits* 和 ThreeDigits* 类型的操作数对二元运算符+ 无效

你能告诉我问题是什么吗?谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

扎心 2024-12-11 12:40:05

您正在尝试对指向对象的指针而不是对象本身进行求和。要调用重载运算符,您必须在对象上调用它,从而取消对指针的引用。

顺便说一句,用 new 创建所有这些对象对于 C++ 来说是一种糟糕的方式;在 C++ 中,与 Java/C# 不同,只有在必要时才应该使用 new,并在堆栈上分配所有其余的内容。让 operator+ 返回指向新创建对象的指针是令人厌恶的。

编写代码的 C++ 方式是:

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;

顺便说一下,重载算术运算符的常用方法是首先重载分配版本(+=、-=、...),然后在它们之上构建“正常”版本。有关运算符重载的详细信息,请参阅运算符重载常见问题解答

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.

日久见人心 2024-12-11 12:40:05

要使用书面的运算符,您需要编写:ThreeDigits* result=*first+*second; 以便取消引用指针。

但是,您的运算符至少有两个问题:第一,它违反了规范的 operator+ 按值返回的最小意外原则。其次,它返回一个指向新内存的指针,除非小心谨慎,否则在各种情况下很可能会泄漏该内存。

看起来您可能有 Java 背景,其中所有内容都是引用计数的。相反,惯用的 C++ 实现将如下所示:

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

并使用:

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

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;
听风念你 2024-12-11 12:40:05

您没有取消引用指针。试试这个:

ThreeDigits* result = *first + *second;

You are not dereferencing the pointers. Try this:

ThreeDigits* result = *first + *second;
我三岁 2024-12-11 12:40:05

您的运算符接受两个 ThreeDigits 引用并返回一个 ThreeDigits 指针。

要使用书面的运算符,您必须取消引用 firstsecond

ThreeDigits* result = *first + *second;

不过,该运算符有一个不寻常的签名。 operator+ 应该返回一个三位数(即不是指向一的指针)。对于您的操作员来说,隐式新(或 malloc)数据是非常糟糕的做法,因为用户不会期望这样做。 RVO 意味着归还副本并不是什么大问题。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文