“return”:无法从“Complex”转化为“Complex &”

文章讲述了在C++中遇到的关于复数类Complex运算符重载的问题,即如何将非成员非友元的加法运算符函数改为友元函数以实现正确类型转换。通过修改友元函数定义解决了返回类型问题。

一、参考资料

二、问题描述

在进行C++作业中遇到了“return”:无法从“Complex”转化为“Complex &”此类问题。

作业题目:

        面对对象程序设计之运算符重载(1)定义一个复数类Complex,重载运算符“+”使之能用于复数的加法运算。将运算符函数重载为非成员、非友元的普通函数。编写程序,求两个复数之和。

自己做的代码如下:

#include<iostream>
using namespace std;
class Complex {
public:
	Complex() { real = 0; imag = 0; }
	Complex(double r, double i) { real = r; imag = i; }
	friend Complex& operator + (Complex & c1, Complex & c2);
	void display();
private:
	double real; double imag;
};
void Complex::display() {
	if (imag>=0)
	cout << "(" << real << "+" << imag << "I)" << endl;
	else
		cout << "(" << real <<  imag << "I)" << endl;
}
Complex& operator+(Complex& c1, Complex& c2) {
	return Complex(c1.real + c2.real, c1.imag + c2.imag);
}
int main() {
	Complex c1(3, 4), c2(5, 6),c3;
	c3 = c1 + c2;
	cout << "c1="; c1.display();
	cout << "c2="; c2.display();
	cout << "c3="; c3.display();
		return 0;
}

三、出错原因

暂时无法找到。

四、解决方案

不过,查找书籍发现在友元函数定义那一块有点缺点,于是进行修改

class Complex {
public:
    Complex() { real = 0; imag = 0; }
    Complex(double r, double i) { real = r; imag = i; }
    friend Complex operator + (Complex  c1, Complex  c2);
    void display();
private:
    double real; double imag;
};

Complex operator+(Complex c1, Complex c2) {
    return Complex(c1.real + c2.real, c1.imag + c2.imag);}

这样return就能返回同类型的Complex了。

using namespace std; class Complex { private: float re, im; //default is double public: Complex(float re = 0, float im = 0): re(re), im(im){}; Complex&amp; operator = (const Complex&amp;); Complex&amp; operator += (const Complex&amp;); Complex&amp; operator -= (const Complex&amp;); Complex&amp; operator *= (const Complex&amp;); Complex&amp; operator /= (const Complex&amp;); Complex&amp; operator = (float); Complex&amp; operator += (float); Complex&amp; operator -= (float); Complex&amp; operator *= (float); Complex&amp; operator /= (float); float real() const { return re; } float imag() const { return im; } friend float real(const Complex&amp;); friend float imag(const Complex&amp;); friend float abs(const Complex&amp;); friend float arg(const Complex&amp;); friend Complex polar(float, float); friend float norm(const Complex&amp;); friend Complex conj(const Complex&amp; x); friend Complex cos(const Complex&amp; x); friend Complex cosh(const Complex&amp; x); friend Complex exp(const Complex&amp; x); // friend Complex log(const Complex&amp; x); friend Complex sin(const Complex&amp; x); friend Complex sinh(const Complex&amp; x); // friend Complex pow(const Complex&amp;, const Complex&amp;); // friend Complex pow(const Complex&amp;, double); // friend Complex pow(double, const Complex&amp;); friend Complex pow(const Complex&amp;, int); // friend Complex sqrt(const Complex&amp; x); friend Complex operator + (const Complex&amp;); friend Complex operator - (const Complex&amp;); friend Complex operator + (const Complex&amp;, const Complex&amp;); friend Complex operator + (const Complex&amp;, float); friend Complex operator + (float, const Complex&amp;); friend Complex operator - (const Complex&amp;, const Complex&amp;); friend Complex operator - (const Complex&amp;, float); friend Complex operator - (float, const Complex&amp;); friend Complex operator * (const Complex&amp;, const Complex&amp;); friend Complex operator * (const Complex&amp;, float); friend Complex operator * (float, const Complex&amp;); friend Complex operator / (const Complex&amp;, const Complex&amp;); friend Complex operator / (const Complex&amp;, float); friend Complex operator / (float, const Complex&amp;); friend int operator == (const Complex&amp;, const Complex&amp;); friend int operator == (const Complex&amp;, float); friend int operator == (float, const Complex&amp;); friend int operator != (const Complex&amp;, const Complex&amp;); friend int operator != (const Complex&amp;, float); friend int operator != (float, const Complex&amp;); // friend istream&amp; operator &gt;&gt; (istream&amp;, Complex&amp;); friend ostream&amp; operator &lt;&lt; (ostream &amp;os, const Complex &amp;val); }; inline Complex&amp; Complex::operator = (const Complex&amp; r) { re = r.re; im = r.im; return *this; } inline Complex&amp; Complex::operator += (const Complex&amp; r) { re += r.re; im += r.im; return *this; } inline Complex&amp; Complex::operator -= (const Complex&amp; r) { re -= r.re; im -= r.im; return *this; } inline Complex&amp; Complex::operator *= (const Complex&amp; r) { float f = re * r.re - im * r.im; im = re * r.im + im * r.re; re = f; return *this; } inline Complex&amp; Complex::operator /= (const Complex&amp; y) { float ar = fabs(y.re); float ai = fabs(y.im); float nr, ni; float t, d; if (ar &lt;= ai) { t = y.re / y.im; d = y.im * (1 + t * t); nr = (re * t + im) / d; ni = (im * t - re) / d; } else { t = y.im / y.re; d = y.re * (1 + t * t); nr = (re + im * t) / d; ni = (im - re * t) / d; } re = nr; im = ni; return *this; } inline Complex&amp; Complex::operator = (float x) { re = x; im = 0.0; return *this; } inline Complex&amp; Complex::operator += (float x) { re += x; return *this; } inline Complex&amp; Complex::operator -= (float x) { re -= x; return *this; } inline Complex&amp; Complex::operator *= (float x) { re *= x; im *= x; return *this; } inline Complex&amp; Complex::operator /= (float x) { re /= x; im /= x; return *this; } inline float real(const Complex&amp; x) { return x.real(); } inline float imag(const Complex&amp; x) { return x.imag(); } inline float abs(const Complex&amp; x) { return _hypotf(real(x), imag(x)); } inline float arg(const Complex&amp; x) { return atan2(imag(x), real(x)); // atan2 returns a value in the range -pi to pi radians } inline Complex polar(float r, float t) { return Complex(r * cos(t), r * sin(t)); } inline float norm(const Complex&amp; x) { return real(x) * real(x) + imag(x) * imag(x); // returns magnitude square } inline Complex conj(const Complex&amp; x) { return Complex(real(x), -imag(x)); } inline Complex cos(const Complex&amp; x) { return Complex(cos(real(x)) * cosh(imag(x)), -sin(real(x)) * sinh(imag(x))); } inline Complex cosh(const Complex&amp; x) { return Complex(cosh(real(x)) * cos(imag(x)), sinh(real(x)) * sin(imag(x))); } inline Complex exp(const Complex&amp; x) { return polar(float(exp(real(x))), imag(x)); } //inline Complex log(const Complex&amp; x) //{ // return Complex(log(abs(x)), arg(x)); //} inline Complex sin(const Complex&amp; x) { return Complex(sin(real(x)) * cosh(imag(x)), cos(real(x)) * sinh(imag(x))); } inline Complex sinh(const Complex&amp; x) { return Complex(sinh(real(x)) * cos(imag(x)), cosh(real(x)) * sin(imag(x))); } //inline Complex pow(const Complex&amp; x, const Complex&amp; y) //{ // double logr = log(abs(x)); // double t = arg(x); // // return polar(double(exp(logr * real(y) - imag(y) * t)), double(imag(y) * logr + real(y) * t)); //} // //inline Complex pow(const Complex&amp; x, double y) //{ // return exp(double(y) * log(x)); //} // //inline Complex pow(double x, const Complex&amp; y) //{ // return exp(y * double(log(x))); //} inline Complex pow(const Complex&amp; xin, int y) { if (y == 0) return Complex(1.0); Complex r(1.0); Complex x(xin); if (y &lt; 0) { y = -y; x = 1/x; } for (;;) { if (y &amp; 1) r *= x; if (y &gt;&gt;= 1) x *= x; else return r; } } //inline Complex sqrt(const Complex&amp; x) //{ // double r = abs(x); // double nr, ni; // // if (r == 0.0) // nr = ni = r; // else if (real(x) &gt; 0) // { // nr = sqrt(0.5 * (r + real(x))); // ni = imag(x) / nr / 2; // } // else // { // ni = sqrt(0.5 * (r - real(x))); // // if (imag(x) &lt; 0) // ni = -ni; // // nr = imag(x) / ni / 2; // } // // return Complex(nr, ni); //} inline Complex operator + (const Complex&amp; x) { return x; } inline Complex operator - (const Complex&amp; x) { return Complex (-real(x), -imag(x)); } inline Complex operator + (const Complex&amp; x, const Complex&amp; y) { return Complex(real(x) + real(y), imag(x) + imag(y)); } inline Complex operator + (const Complex&amp; x, float y) { return Complex(real(x) + y, imag(x)); } inline Complex operator + (float x, const Complex&amp; y) { return Complex(x + real(y), imag(y)); } inline Complex operator - (const Complex&amp; x, const Complex&amp; y) { return Complex(real(x) - real(y), imag(x) - imag(y)); } inline Complex operator - (const Complex&amp; x, float y) { return Complex(real(x) - y, imag(x)); } inline Complex operator - (float x, const Complex&amp; y) { return Complex(x - real(y), -imag(y)); } inline Complex operator * (const Complex&amp; x, const Complex&amp; y) { return Complex(real(x) * real(y) - imag(x) * imag(y), real(x) * imag(y) + imag(x) * real(y)); } inline Complex operator * (const Complex&amp; x, float y) { return Complex(real(x) * y, imag(x) * y); } inline Complex operator * (float x, const Complex&amp; y) { return Complex(x * real(y), x * imag(y)); } //inline Complex operator / (const Complex&amp; x, const Complex&amp; y) //{ // return Complex(real(x) * real(y) + imag(x) * imag(y), -real(x) * imag(y) + imag(x) * real(y)) / norm(y); //} inline Complex operator / (const Complex&amp; x, const Complex&amp; y) { float ar = fabs(real(y)); float ai = fabs(imag(y)); float nr, ni; float t, d; if (ar &lt;= ai) { t = real(y) / imag(y); d = imag(y) * (1 + t * t); nr = (real(x) * t + imag(x)) / d; ni = (imag(x) * t - real(x)) / d; } else { t = imag(y) / real(y); d = real(y) * (1 + t * t); nr = (real(x) + imag(x) * t) / d; ni = (imag(x) - real(x) * t) / d; } return Complex(nr, ni); } inline Complex operator / (const Complex&amp; x, float y) { return Complex(real(x) / y, imag(x) / y); } inline Complex operator / (float x, const Complex&amp; y) { float d = norm(y); return Complex((x * real(y)) / d, (-x * imag(y)) / d); } inline int operator == (const Complex&amp; x, const Complex&amp; y) { return real(x) == real(y) &amp;&amp; imag(x) == imag(y); } inline int operator == (const Complex&amp; x, float y) { return real(x) == y &amp;&amp; imag(x) == 0; } inline int operator == (float x, const Complex&amp; y) { return x == real(y) &amp;&amp; imag(y) == 0; } inline int operator != (const Complex&amp; x, const Complex&amp; y) { return real(x) != real(y) || imag(x) != imag(y); } inline int operator != (const Complex&amp; x, float y) { return real(x) != y || imag(x) != 0; } inline int operator != (float x, const Complex&amp; y) { return x != real(y) || imag(y) != 0; } //inline istream&amp; operator &gt;&gt; (istream&amp; is, Complex&amp; x) //{ // double re, im = 0; // char ch = 0; // // if (is.ipfx()) // { // if (is.peek() == &#39;(&#39;) // is &gt;&gt; ch; // // is &gt;&gt; re; // // if (ch == &#39;(&#39;) // { // is &gt;&gt; ch; // // if (ch == &#39;,&#39;) // is &gt;&gt; im &gt;&gt; ch; // } // } // // is.isfx(); // // if (ch != 0 &amp;&amp; ch != &#39;)&#39;) // is.setstate(ios::failbit); // else if (is.good()) // x = Complex(re, im); // // return is; //} // inline ostream&amp; operator &lt;&lt; (ostream &amp;os, const Complex &amp;val) { cout &lt;&lt; val.real(); if (val.imag() &lt; 0.0) cout &lt;&lt; &quot; - &quot; &lt;&lt; -val.imag() &lt;&lt; &quot;i&quot;; else cout &lt;&lt; &quot; + &quot; &lt;&lt; val.imag() &lt;&lt; &quot;i&quot;; return os; } 修改这段代码,要求把complex类转化到一个新的namespace, 以避免于std冲突
最新发布
11-21
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值