【问题标题】:Cannot Overload + Operator in Class C++不能在 C++ 类中重载 + 运算符
【发布时间】:2021-02-17 19:52:12
【问题描述】:

我正在尝试重载加法运算符。

#pragma once
#include <fstream>
#include <iostream>
class ComplexNumber
{
private:
    int* real;
    int* imag;
public:
    ComplexNumber();
    ~ComplexNumber();
    ComplexNumber(const ComplexNumber&);
    ComplexNumber& operator=(const ComplexNumber&);
    friend std::istream& operator>>(std::istream&, ComplexNumber&);
    friend std::ostream& operator<<(std::ostream&, const ComplexNumber&);
    ComplexNumber operator+(const ComplexNumber&);
    friend ComplexNumber operator-(const ComplexNumber&, const ComplexNumber&);
    ComplexNumber operator* (const ComplexNumber&);
    friend ComplexNumber operator /(const ComplexNumber&, const ComplexNumber&);
};

ComplexNumber::ComplexNumber()
{
    real = new int{ 10 };
    imag = new int{ 10 };
}
ComplexNumber ComplexNumber::operator+(const ComplexNumber& a)
{
    ComplexNumber temp;
    temp.real = this->real + a.real;
    temp.imag = this->imag + a.imag;
    return temp;
    
}

当我尝试编译代码时,它在 a.real 和 a.imag 上给我一个错误,说表达式必须具有整数或无范围枚举类型。这是什么意思?感谢任何提前提供帮助的人。

编辑


std::ostream& operator<<(std::ostream& out, const ComplexNumber& a)
{
    out << *(a.real) << " " << *(a.imag) << "i"; 
    return out;
}

ComplexNumber  aa, ab, ac;
ac = aa + ab;
std::cout << ac << std::endl;

这仍然输出 10 和 10i 而不是 20 和 20i

EDIT2:

ComplexNumber& ComplexNumber::operator=(const ComplexNumber& a)
{
    ComplexNumber temp;
    *(temp.real) = *(a.real);
    *(temp.imag) = *(a.imag);
    return temp;
}

【问题讨论】:

  • 为什么realimag的类型是int*而不是int类型?
  • 我的教授向我们提供了头文件,我们必须实现它。这是唯一的原因哈哈
  • 你能发布你对operator=的实现吗?
  • 是的,我刚刚添加了它。感谢您的帮助
  • @ZachSal 你在做operator=时不应该使用temp,你需要通过*this-&gt;real = *a.real;分配给当前对象。看at my answer

标签: c++ class dynamic operator-overloading


【解决方案1】:

只需通过* 运算符取消引用您的real/imag 指针,如下面的代码所示。或者,也许您只需将它们存储为普通的 int(例如 int real;)而不是指向 int 的指针。

Try it online!

#include <fstream>
#include <iostream>

class ComplexNumber
{
private:
    int* real;
    int* imag;
public:
    ComplexNumber();
    ~ComplexNumber() {}
    ComplexNumber(const ComplexNumber&);
    ComplexNumber& operator=(const ComplexNumber&);
    friend std::istream& operator>>(std::istream&, ComplexNumber&);
    friend std::ostream& operator<<(std::ostream&, const ComplexNumber&);
    ComplexNumber operator+(const ComplexNumber&);
    friend ComplexNumber operator-(const ComplexNumber&, const ComplexNumber&);
    ComplexNumber operator* (const ComplexNumber&);
    friend ComplexNumber operator /(const ComplexNumber&, const ComplexNumber&);
};

ComplexNumber::ComplexNumber()
{
    real = new int{ 10 };
    imag = new int{ 10 };
}
ComplexNumber ComplexNumber::operator+(const ComplexNumber& a)
{
    ComplexNumber temp;
    *temp.real = *this->real + *a.real;
    *temp.imag = *this->imag + *a.imag;
    return temp;
    
}
ComplexNumber & ComplexNumber::operator=(const ComplexNumber& a)
{
    *real = *a.real;
    *imag = *a.imag;
    return *this;
    
}
std::ostream& operator<<(std::ostream& out, const ComplexNumber& a)
{
    out << *(a.real) << " " << *(a.imag) << "i"; 
    return out;
}

int main() {
    ComplexNumber aa, ab, ac;
    ac = aa + ab;
    std::cout << ac << std::endl;
    return 0;
}

输出:

20 20i

【讨论】:

  • 哦,好的,谢谢。只是好奇,如果它指向一个新的 int,为什么我需要取消引用?
  • 当我尝试在 main 中添加两个类对象时,它们似乎没有添加。你介意看看我的编辑吗?
  • @ZachSal 首先关于您的评论 - 如果您的类型是 int * 并且您拥有这种类型,则始终需要通过 * 取消引用,无论它如何初始化,您都需要使用 *到处。关于第二条评论,我现在会查看您的编辑...
  • @ZachSal 可能你错误​​地实现了operator=,我编辑了我的答案并添加了所有必要的代码,它在我的代码中输出20 20i,看看!
猜你喜欢
  • 1970-01-01
  • 2011-11-29
  • 1970-01-01
  • 2011-12-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-10
相关资源
最近更新 更多