0
[待解决问题]

# 我不理解为什么这里会编译通不过啊？？

``````#include "m_complex.h"
#include <iostream>
using namespace std;
int main()
{
Complex a(3.0, 4.0);
Complex c;
cout << "Enter a complex number (q to quit): \n";
while (cin >> c)
{
cout << "c is " << c << '\n';
cout << "complex conjuage is " << ~c << '\n';
cout << " a is " << a << '\n';
cout << "a +  c is " << a + c << '\n';
cout << "a - c is " << a - c << '\n';
cout << "a * c is " << a * c << '\n';
cout << "2 * c is " << 2 * c << '\n';
cout << "Enter a complex number (q to quit): \n";
}
cout << "Done!\n";
return 0;
}
``````
``````#pragma once
#include <iostream>
class Complex
{
public:
Complex();
Complex(double m_real, double m_vitual);
Complex operator+(const Complex& obj) const;
Complex operator-(const Complex& obj) const;
Complex operator*(const Complex& obj) const;
Complex operator*(double m) const;
Complex operator~() const;
void reset(double lhd, double rhd);
friend Complex operator*(double lhd, Complex& rhd);
friend std::ostream& operator<<(std::ostream& os, Complex& rhd);
friend std::istream& operator>>(std::istream& is, Complex& rhd);
private:
double real; //实数部分
double vitual; //虚数部分
};
``````
``````#include "m_complex.h"

Complex::Complex()
{
real = 0;
vitual = 0;
}

Complex::Complex(double m_real, double m_vitual)
{
real = m_real;
vitual = m_vitual;
}

Complex Complex::operator+(const Complex& obj) const
{
double t_real = this->real + obj.real;
double t_vitual = this->vitual + obj.vitual;
return Complex(t_real, t_vitual);
}

Complex Complex::operator-(const Complex& obj) const
{
double t_real = this->real - obj.real;
double t_vitual = this->vitual - obj.vitual;
return Complex(t_real, t_vitual);
}

Complex Complex::operator*(const Complex& obj) const
{
double t_real = this->real * obj.real - this->vitual * obj.vitual;
double t_vitual = this->real * obj.real + this->vitual * obj.vitual;
return Complex(t_real, t_vitual);
}

Complex Complex::operator*(double m) const
{
return Complex(m * this->real, m * this->vitual);
}

Complex Complex::operator~() const
{
return Complex(this->real, -this->vitual);
}

void Complex::reset(double lhd, double rhd)
{
real = lhd;
vitual = rhd;
}

Complex operator*(double lhd, Complex& rhd)
{
return Complex(lhd * rhd.real, lhd * rhd.vitual);
}

std::ostream& operator<<(std::ostream& os, Complex& rhd)
{
// TODO: 在此处插入 return 语句
os << "(" << rhd.real << "," << rhd.vitual << ")" << std::endl;
return os;
}

std::istream& operator>>(std::istream& is, Complex& rhd)
{
// TODO: 在此处插入 return 语句
double m_real = 0, m_vitual = 0;
std::cout << "input real number: " << std::endl;
is >> m_real;
std::cout << "input vitual number: " << std::endl;
is >> m_vitual;
rhd.reset(m_real, m_vitual);
return is;
}
``````
HfAndSi02 | 菜鸟二级 | 园豆：202

0

0

c++早都忘了，赶紧换语言，不然就晚了

0

1 你把所有的计算操作的结果用一个右值引用保存下来
cout << "a + c is " << a + c << '\n';

Complex &&ac = a + c;
cout << "a + c is " << ac << '\n';

2 增加处理右值的<<重载函数
friend std::ostream& operator<<(std::ostream& os, Complex&& rhd);

lubiangou | 园豆：202 (菜鸟二级) | 2023-03-10 15:03
1

FayeValentine | 园豆：208 (菜鸟二级) | 2023-03-28 16:45

您需要登录以后才能回答，未注册用户请先注册