虚数类出错?请教大虾!谢谢!

luoxiang2000 2003-02-08 11:37:54
#include "stdafx.h"
#include <iostream.h>
class complex{
public:
complex(double r, double i) {real = r; imaginary = i;}
void operator+(complex c);
void operator-(complex c);
void operator*(complex c);
friend void operator+(complex c1, complex c2);
friend void operator-(complex c1, complex c2);
friend void operator*(complex c1, complex c2);
friend void print(complex c);
private:
double real, imaginary;
};

void operator+(complex c1, complex c2)
{
c1.real = c1.real + c2.real;
c1.imaginary = c1.imaginary + c2.imaginary;
return;
}

void complex::operator*(complex c)
{
real = real * c.real + imaginary * c.imaginary*(-1);
imaginary = real * c.imaginary + imaginary * c.real;
return;
}

void print(complex c)
{
cout<<c.real<<'+'<<c.imaginary<<'i'
<<'\n';
}

int main(int argc, char* argv[])
{
double real1,imaginary1,real2,imaginary2;
cout<<"请输入复数(如:1+2i):"<<endl;
//for (int i=1;i<3;i++) {
cout<<"请输入实部:"<<endl;
cin>>real1>>real2;
cout<<"请输入虚部:"<<endl;
cin>>imaginary1>>imaginary2;
//}
complex a(real1,imaginary1),b(20,20),c(real2, imaginary2);
a=b+c; //在这里报错:'operator +' is ambiguous
print(a); print(b); print(c);
return 0;
}
...全文
43 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
哥是技术人 2003-02-08
  • 打赏
  • 举报
回复
去!返回Void,还要a=b.operator+(c);
Cybergate 2003-02-08
  • 打赏
  • 举报
回复
不够完整,但参考足够了

class complex {

public:
// constructors
complex(double __re_val, double __im_val=0);
complex();

// complex manipulations
friend double real(complex &); // the real part
friend double imag(complex &); // the imaginary part
friend complex conj(complex &); // the complex conjugate
friend double norm(complex &); // the square of the magnitude
friend double arg(complex &); // the angle in the plane

// Create a complex object given polar coordinates
friend complex polar(double __mag, double __angle=0);

// Overloaded ANSI C math functions
friend double abs(complex &);
friend complex acos(complex &);
friend complex asin(complex &);
friend complex atan(complex &);
friend complex cos(complex &);
friend complex cosh(complex &);
friend complex exp(complex &);
friend complex log(complex &);
friend complex log10(complex &);
friend complex pow(complex & __base, double __expon);
friend complex pow(double __base, complex & __expon);
friend complex pow(complex & __base, complex & __expon);
friend complex sin(complex &);
friend complex sinh(complex &);
friend complex sqrt(complex &);
friend complex tan(complex &);
friend complex tanh(complex &);

// Binary Operator Functions
friend complex operator+(complex &, complex &);
friend complex operator+(double, complex &);
friend complex operator+(complex &, double);
friend complex operator-(complex &, complex &);
friend complex operator-(double, complex &);
friend complex operator-(complex &, double);
friend complex operator*(complex &, complex &);
friend complex operator*(complex &, double);
friend complex operator*(double, complex &);
friend complex operator/(complex &, complex &);
friend complex operator/(complex &, double);
friend complex operator/(double, complex &);
friend int operator==(complex &, complex &);
friend int operator!=(complex &, complex &);
complex & operator+=(complex &);
complex & operator+=(double);
complex & operator-=(complex &);
complex & operator-=(double);
complex & operator*=(complex &);
complex & operator*=(double);
complex & operator/=(complex &);
complex & operator/=(double);
complex operator+();
complex operator-();

// Implementation
private:
double re, im;
};


// Inline complex functions

inline complex::complex(double __re_val, double __im_val)
{
re = __re_val;
im = __im_val;
}

inline complex::complex()
{
/* if you want your complex numbers initialized ...
re = im = 0;
*/
}

inline complex complex::operator+()
{
return *this;
}

inline complex complex::operator-()
{
return complex(-re, -im);
}


// Definitions of compound-assignment operator member functions

inline complex & complex::operator+=(complex & __z2)
{
re += __z2.re;
im += __z2.im;
return *this;
}

inline complex & complex::operator+=(double __re_val2)
{
re += __re_val2;
return *this;
}

inline complex & complex::operator-=(complex & __z2)
{
re -= __z2.re;
im -= __z2.im;
return *this;
}

inline complex & complex::operator-=(double __re_val2)
{
re -= __re_val2;
return *this;
}

inline complex & complex::operator*=(double __re_val2)
{
re *= __re_val2;
im *= __re_val2;
return *this;
}

inline complex & complex::operator/=(double __re_val2)
{
re /= __re_val2;
im /= __re_val2;
return *this;
}


// Definitions of non-member complex functions

inline double real(complex & __z)
{
return __z.re;
}

inline double imag(complex & __z)
{
return __z.im;
}

inline complex conj(complex & __z)
{
return complex(__z.re, -__z.im);
}

inline complex polar(double __mag, double __angle)
{
return complex(__mag*cos(__angle), __mag*sin(__angle));
}


// Definitions of non-member binary operator functions

inline complex operator+(complex & __z1, complex & __z2)
{
return complex(__z1.re + __z2.re, __z1.im + __z2.im);
}

inline complex operator+(double __re_val1, complex & __z2)
{
return complex(__re_val1 + __z2.re, __z2.im);
}

inline complex operator+(complex & __z1, double __re_val2)
{
return complex(__z1.re + __re_val2, __z1.im);
}

inline complex operator-(complex & __z1, complex & __z2)
{
return complex(__z1.re - __z2.re, __z1.im - __z2.im);
}

inline complex operator-(double __re_val1, complex & __z2)
{
return complex(__re_val1 - __z2.re, -__z2.im);
}

inline complex operator-(complex & __z1, double __re_val2)
{
return complex(__z1.re - __re_val2, __z1.im);
}

inline complex operator*(complex & __z1, double __re_val2)
{
return complex(__z1.re*__re_val2, __z1.im*__re_val2);
}

inline complex operator*(double __re_val1, complex & __z2)
{
return complex(__z2.re*__re_val1, __z2.im*__re_val1);
}

inline complex operator/(complex & __z1, double __re_val2)
{
return complex(__z1.re/__re_val2, __z1.im/__re_val2);
}

inline int operator==(complex & __z1, complex & __z2)
{
return __z1.re == __z2.re && __z1.im == __z2.im;
}

inline int operator!=(complex & __z1, complex & __z2)
{
return __z1.re != __z2.re || __z1.im != __z2.im;
}


// Complex stream I/O

ostream & operator<<(ostream &, complex &);
istream & operator>>(istream &, complex &);

luoxiang2000 2003-02-08
  • 打赏
  • 举报
回复
那未能给我一个虚数类的完整源代码的例子吗?
谢谢啦!
danmao 2003-02-08
  • 打赏
  • 举报
回复
void operator+(complex c);
friend void operator+(complex c1, complex c2)

没有返回值?!等号得不到结果呀!!

64,639

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧