重载运算符 C++ 的逻辑错误
Logical error with overloaded operator C++
我一直在制作一个名为 Complex 的 class 来存储和处理复数。我遇到的问题是我的重载 + 和 * 函数 return 一个非常大的数字,我只能假设这与指针有关,但我对使用它们还很陌生。我在这里复制了输出:
c1: 1 - i5
c2: -4 + i3
c1 + c2: -9.25596e+061 - i9.255
c1 * c2: -9.25596e+061 - i9.255
Press any key to continue . . .
这是 class 架构:
// Complex.h
#include <iostream>
#include <cmath>
using namespace std;
class Complex
{
friend std::ostream &operator<<(std::ostream &, const Complex &);
friend std::istream &operator>>(std::istream &, Complex &);
friend Complex &operator+(const Complex &, const Complex &);
friend Complex &operator+(const Complex &, const double &);
friend Complex &operator+(const double &, const Complex &);
friend Complex &operator*(const Complex &, const Complex &);
friend Complex &operator*(const Complex &, const double &);
friend Complex &operator*(const double &, const Complex &);
public:
explicit Complex(double re=0, double im=0);
void setReal(double re);
void setImag(double im);
double &getReal();
double &getImag();
~Complex();
private:
double real;
double imag;
};
这里是我程序的相关部分:
// Complex.cpp
#include "Complex.h"
ostream &operator<<(ostream &output, const Complex &num)
{
output << num.real << (num.imag >= 0 ? " + i" : " - i") << abs(num.imag);
return output;
}
Complex &operator+(const Complex &num1, const Complex &num2)
{
Complex sum(num1.real + num2.real, num1.imag + num2.imag); // Create new complex by summing elements
return sum;
}
Complex &operator*(const Complex &num1, const Complex &num2)
{
Complex prod(num1.real * num2.real - num1.imag * num2.imag, num1.real * num2.imag + num1.imag * num2.real); // Create new complex by expansion
return prod;
}
Complex::Complex(double re, double im)
{
real = re;
imag = im;
}
int main() {
Complex c1(1.0, -5.0);
Complex c2(-4.0, 3.0);
cout << "c1: " << c1 << endl;
cout << "c2: " << c2 << endl;
cout << "c1 + c2: " << c1 + c2 << endl;
cout << "c1 * c2: " << c1 * c2 << endl;
system("pause");
}
不好意思打扰你们。您是否知道某个网站可以很好地解释 C++ 的概念,例如指针、最小权限、多态性、继承等?
很抱歉在这里提出基本问题,但很难找到清楚解释这些内容的资源。
问题是您的大多数重载运算符 return 是对局部 var 的引用,当运算符函数 returns 时它被销毁,留下一个悬空引用。尝试将您的运算符更改为 return a Complex
而不是 Complex &
:
Complex operator+(const Complex &num1, const Complex &num2)
{
Complex sum(num1.real + num2.real, num1.imag + num2.imag); // Create new complex by summing elements
return sum;
}
错误的return类型:
Complex &operator+(const Complex &num1, const Complex &num2)
^^^^^^^^^
您正在创建一个临时 Complex
并通过引用 return 对其进行处理,然后它被销毁,因此您有一个悬空引用。由于 operator+
正在构造一个新对象并 returning 它,它应该 return 它由 value... 所以签名应该是:
Complex operator+(Complex const&, Complex const& );
我一直在制作一个名为 Complex 的 class 来存储和处理复数。我遇到的问题是我的重载 + 和 * 函数 return 一个非常大的数字,我只能假设这与指针有关,但我对使用它们还很陌生。我在这里复制了输出:
c1: 1 - i5
c2: -4 + i3
c1 + c2: -9.25596e+061 - i9.255
c1 * c2: -9.25596e+061 - i9.255
Press any key to continue . . .
这是 class 架构:
// Complex.h
#include <iostream>
#include <cmath>
using namespace std;
class Complex
{
friend std::ostream &operator<<(std::ostream &, const Complex &);
friend std::istream &operator>>(std::istream &, Complex &);
friend Complex &operator+(const Complex &, const Complex &);
friend Complex &operator+(const Complex &, const double &);
friend Complex &operator+(const double &, const Complex &);
friend Complex &operator*(const Complex &, const Complex &);
friend Complex &operator*(const Complex &, const double &);
friend Complex &operator*(const double &, const Complex &);
public:
explicit Complex(double re=0, double im=0);
void setReal(double re);
void setImag(double im);
double &getReal();
double &getImag();
~Complex();
private:
double real;
double imag;
};
这里是我程序的相关部分:
// Complex.cpp
#include "Complex.h"
ostream &operator<<(ostream &output, const Complex &num)
{
output << num.real << (num.imag >= 0 ? " + i" : " - i") << abs(num.imag);
return output;
}
Complex &operator+(const Complex &num1, const Complex &num2)
{
Complex sum(num1.real + num2.real, num1.imag + num2.imag); // Create new complex by summing elements
return sum;
}
Complex &operator*(const Complex &num1, const Complex &num2)
{
Complex prod(num1.real * num2.real - num1.imag * num2.imag, num1.real * num2.imag + num1.imag * num2.real); // Create new complex by expansion
return prod;
}
Complex::Complex(double re, double im)
{
real = re;
imag = im;
}
int main() {
Complex c1(1.0, -5.0);
Complex c2(-4.0, 3.0);
cout << "c1: " << c1 << endl;
cout << "c2: " << c2 << endl;
cout << "c1 + c2: " << c1 + c2 << endl;
cout << "c1 * c2: " << c1 * c2 << endl;
system("pause");
}
不好意思打扰你们。您是否知道某个网站可以很好地解释 C++ 的概念,例如指针、最小权限、多态性、继承等?
很抱歉在这里提出基本问题,但很难找到清楚解释这些内容的资源。
问题是您的大多数重载运算符 return 是对局部 var 的引用,当运算符函数 returns 时它被销毁,留下一个悬空引用。尝试将您的运算符更改为 return a Complex
而不是 Complex &
:
Complex operator+(const Complex &num1, const Complex &num2)
{
Complex sum(num1.real + num2.real, num1.imag + num2.imag); // Create new complex by summing elements
return sum;
}
错误的return类型:
Complex &operator+(const Complex &num1, const Complex &num2)
^^^^^^^^^
您正在创建一个临时 Complex
并通过引用 return 对其进行处理,然后它被销毁,因此您有一个悬空引用。由于 operator+
正在构造一个新对象并 returning 它,它应该 return 它由 value... 所以签名应该是:
Complex operator+(Complex const&, Complex const& );