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

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;
}
...全文
86 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/387d7c8f6289 在使用IE浏览器的过程中,偶尔会出现重置为默认配置操作未能顺利完成的现象。这种情况的发生可能源于多种因素,包括注册表项出现偏差、浏览器附加组件之间存在不兼容、操作系统授权设置不当或受到恶意程序影响等。针对这一挑战,我们可以通过一系列系统性的方法来逐一诊断和纠正。首先需要验证当前用户是否具备管理员级别的操作权限。在执行重置配置的操作时,必须确保IE浏览器是以管理员身份启动的。具体操作方式为,使用鼠标右键单击IE程序图标,并从弹出的菜单中选择“以管理员身份运行”这一选项。其次,应当检查是否存在恶意软件。恶意程序有时会非法修改浏览器的配置信息,从而阻碍重置过程的正常进行。用户可以借助Windows系统自带的Microsoft Safety Scanner进行检测,或者选择安装并使用第三方安全防护软件执行全面的系统扫描。紧接着,需要对浏览器的缓存数据以及Cookies文件进行清理。在IE浏览器界面中,通过访问“工具”菜单并选择“Internet选项”,然后在弹出的设置界面中,切换至“常规”标签页,并执行清除浏览历史记录的操作,此过程应包含Cookies和临时互联网文件。倘若上述步骤均未能解决问题,那么可以考虑禁用或移除那些可能引发冲突的浏览器附加组件。相关操作需要在“工具”菜单下找到并打开“管理加载项”,对可疑的插件进行禁用处理,并在浏览器重新启动后再次尝试执行重置操作。再者,修复IE浏览器相关的注册表配置也是至关重要的环节。注册表编辑器是一个功能强大的系统工具,任何不当的操作都可能导致严重的系统问题,因此在进行修改前必须创建系统还原点作为安全备份。用户可以通过按下“Win+R”组合...
内容概要:本文详细介绍了光伏储能虚拟同步发电机(VSG)并网仿真模型的Simulink实现方法,系统研究了光伏发电与储能系统融合VSG技术并入电网的动态特性与控制策略。通过构建完整的VSG控制模块,模拟其在并网过程中的虚拟惯性响应、频率调节能力和有功无功功率支撑特性,验证了该系统在增强电网稳定性、提升新能源并网适应性方面的有效性。模型全面涵盖光伏出力特性、储能系统充放电管理、VSG有功-无功解耦控制、下垂控制机制以及并网点动态响应等关键环节,并通过多种工况下的仿真分析,评估系统在不同运行条件下的动态性能与鲁棒性。; 适合人群:具备电力系统、新能源发电、自动控制等相关专业背景,熟悉Matlab/Simulink仿真环境,从事新能源并网、微电网控制或电力电子系统研究的科研人员、工程技术人员及研究生。; 使用场景及目标:①深入理解光伏储能系统如何通过VSG技术模拟同步发电机的惯性和阻尼特性以支持电网稳定;②掌握虚拟惯量控制、下垂控制等VSG核心控制策略在Simulink中的建模、参数设计与实现方法;③为新型电力系统中新能源并网的动态仿真、稳定性分析及控制策略优化提供可靠的模型基础和技术参考。; 阅读建议:建议结合所提供的仿真模型进行逐步操作与参数调试,重点关注VSG控制环路的结构设计、控制器参数整定及其对系统动态性能的影响,同时可将该模型拓展应用于多机并联、微电网孤岛/并网切换、多能源协同控制等更复杂的系统场景中进行进阶研究。

65,214

社区成员

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

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