[go: up one dir, main page]

首页 新闻 会员 周边 捐助

C++重载问题??求教!!

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的主页 HfAndSi02 | 菜鸟二级 | 园豆:202
提问于:2023-01-29 18:04
< >
分享
所有回答(4)
0

代码整体没啥毛病,你重载<<输出符后,返回的是cout的一个引用,后面你又重载了+,也就是变成了,cout+??,但是并没有对应的重载函数(这是我的第一个猜想);
第二个猜想我觉得更有可能,那就是代码确实先执行了你的运算操作并且返回了一个对象类型,但是你的<<输出符号前面是char*类型的,也就是说代码变成了ostream<<(char * arr,Complex& rhd),,所以报错,你试试弄个临时变量计算之后打印那个临时变量。
我也是小菜鸟,如果有不对的地方请多多指教,共勉。

牛刀小胜 | 园豆:202 (菜鸟二级) | 2023-02-26 10:50
0

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

先娶国王后取经 | 园豆:46 (初学一级) | 2023-03-03 16:56
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

打印所有运算时记得加个括号,否则编译器会理解为cout的符号重载

FayeValentine | 园豆:208 (菜鸟二级) | 2023-03-28 16:45
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册