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

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;
}
...全文
82 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)

没有返回值?!等号得不到结果呀!!
下载代码方式:https://pan.quark.cn/s/dd3561eca308 在软件开发领域,面向对象编程(OOP)是一种普遍采纳的结构化方法,它使得开发者能够借助模拟现实环境中的实体和关系来构建软件系统。在本案例中,我们观察到的是一个关于借助抽象来执行不同几何图形面积求解的实践应用。现在,让我们详细分析这一议题。 标题 "应用抽象计算面积" 清晰地表明我们将要讨论一个抽象,此设定了一个用于测量图形面积的标准函数,但并未提供实际的执行过程。抽象在诸如C#或Java等编程语言中通常借助`abstract`修饰符进行声明,它们无法直接创建对象实例,仅能作为其他的基础模板。 描述部分提及的"图形界面应用"暗示这是一个基于视觉用户界面(GUI)的系统,可能运用了.NET Framework的Windows Forms或WPF技术,或者是Java平台的Swing或JavaFX框架。在这样环境下,用户能够通过视觉元素与这些几何体进行互动,例如输入相关尺寸并观看到计算得出的面积值。 抽象“几何体”内嵌了“计算面积”这一抽象函数。在代码层面,这可以被表述为: ```csharp public abstract class GeometricShape { public abstract double CalculateArea(); } ``` 随后,有三个派生:圆(Circle)、矩形(Rectangle)和三角形(Triangle),它们各自提供了这个抽象函数的具体实现。比如,圆的面积是通过π乘以半径的平方得到的,矩形的面积是长和宽的乘积,而三角形的面积可能是底乘以高再除以2的结果。这些将提供具体实现来计算它们各自的面积: ```csharp p...
内容概要:本文系统研究了移相控制全桥LLC谐振变换器的工作特性,深入分析其在不同工作模式下的运行机理与性能表现,重点探讨了软开关实现、高效率能量转换及宽范围电压调节等关键技术优势。通过Simulink搭建精确的仿真模型,对谐振腔参数、开关频率、电压增益、系统效率等关键指标进行仿真分析,验证了理论设计的正确性。同时,详细研究了移相控制策略对系统动态响应、稳定性和轻载/重载工况适应性的影响,揭示了控制参数与电路参数之间的耦合关系,为高频高效电源设计提供了理论依据和实践指导。; 适合人群:具备电力电子技术、模拟电路及自动控制理论基础,从事开关电源、新能源变换器、电动汽车充电模块或高频电源系统研发的工程师及高校研究生。; 使用场景及目标:①掌握全桥LLC谐振变换器的拓扑结构、工作原理与关键参数设计方法;②理解移相控制在实现零电压开通(ZVS)和零电流关断(ZCS)中的作用机制;③通过Simulink仿真掌握变换器建模、参数优化与性能评估流程,服务于实际产品开发与学术课题研究。; 阅读建议:建议读者结合提供的Simulink仿真模型进行同步操作,重点关注谐振网络(Lr, Lm, Cr)参数与移相角之间的匹配设计,深入理解软开关条件的形成过程,并通过调整负载和输入电压进行多工况仿真,以全面掌握系统动态特性。

65,211

社区成员

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

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