设为首页收藏本站

新微赢技术网

 找回密码
 注册
搜索
热搜: 回贴
查看: 1547|回复: 0
打印 上一主题 下一主题

运算符重载错误

[复制链接]
跳转到指定楼层
1#
发表于 2009-11-4 00:27:36 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
#include<math.h>
#include"string.h"
#include "stdafx.h"
class CComplex
{
public:
CComplex();
CComplex(double dblx,double dbly);
CComplex(const CComplex&other);
virtual ~CComplex(){};
void Setreal(double dblx);
void Setimag(double dbly);
double Getreal();
double Getimag();
CString ToString() const;
void FromString(CString s,const CString& sDelim=" ");
bool operator==(const CComplex &cpxx) const;
bool operator!=(const CComplex &cpxx) const;
CComplex& operator=(const CComplex &cpxx);
CComplex operator+(const CComplex &cpxx) const;
CComplex operator-(const CComplex &cpxx) const;
CComplex operator*(const CComplex &cpxx) const;
CComplex operator/(const CComplex &cpxx) const;
double Abs() const;
void Root(int n,CComplex cpxr[]) const;
CComplex Pow(double dblw) const;
CComplex Pow(CComplex cpxw,int n=0) const;
CComplex Log() const;
CComplex Sin() const;
CComplex Cos() const;
CComplex Tan() const;
protected:
double m_dblx;
double m_dbly;
};
CComplex::CComplex()
{
m_dblx=0.0;
m_dbly=0.0;
}
CComplex::CComplex(double dblx,double dbly)
{
m_dblx=dblx;
m_dbly=dbly;
}
CComplex::CComplex(const CComplex &other)
{
m_dblx=other.m_dblx;
m_dbly=other.m_dbly;
}
void CComplex::Setreal(double dblx)
{
m_dblx=dblx;
}
void CComplex::Setimag(double dbly)
{
m_dbly=dbly;
}
double CComplex::Getreal()
{
return m_dblx;
}
double CComplex::Getimag()
{
return m_dbly;
}
CString CComplex::ToString() const
{
CString s;
if(m_dblx!=0.0)
{
if(m_dbly>0)
s.Format("%f+%fj",m_dblx,m_dbly);
else if(m_dbly<0)
s.Format("%f-%fj",m_dblx,fabs(m_dbly));
else
s.Format("%f",m_dblx);
}
else
{
if(m_dbly>0)
s.Format("%f",m_dbly);
else if(m_dbly<0)
s.Format("-%fj",fabs(m_dbly));
else
s.Format("%f",m_dblx);
}
return s;
}
void CComplex::FromString(CString s,const CString &sDelim)
{
int npos=s.Find(sDelim);
if(npos==-1)
{
s.TrimLeft();
s.TrimRight();
m_dblx=atof(s);
m_dbly=0;
}
else
{
int nlen=s.GetLength();
CString sleft=s.Left(npos);
CString sright=s.Right(nlen-npos-1);
sleft.TrimLeft();
sright.TrimRight();
m_dblx=atof(sleft);
m_dbly=atof(sright);
}
}

bool CComplex::operator ==(const CComplex & cpxx) const
{
return (m_dblx==cpxx.m_dblx&&m_dbly==cpxx.m_dbly);
}

bool CComplex::operator !=(const CComplex & cpxx) const
{
return (m_dblx!=cpxx.m_dblx || m_dbly!=cpxx.m_dbly);
}
CComplex& CComplex::operator =(const CComplex & cpxx)
{
m_dblx=cpxx.m_dblx;
m_dbly=cpxx.m_dbly;
return *this;
}
CComplex CComplex::operator +(const CComplex& cpxx) const
{
double x=m_dblx+cpxx.m_dblx;
double y=m_dbly+cpxx.m_dbly;

return CComplex(x,y);
}
CComplex CComplex::operator -(const CComplex & cpxx) const
{
double x=m_dblx-cpxx.m_dblx;
double y=m_dbly-cpxx.m_dbly;
return CComplex(x,y);
}
CComplex CComplex::operator *(const CComplex& cpxx) const
{
double x=m_dblx*cpxx.m_dblx-m_dbly*cpxx.m_dbly;
double y=m_dblx*cpxx.m_dbly-m_dbly*cpxx.m_dblx;
return CComplex(x,y);
}
double CComplex::Abs() const
{
double x=fabs(m_dblx);
double y=fabs(m_dbly);
if(m_dblx==0)
return y;
if(m_dbly==0)
return x;
if (x>y)
return (x*sqrt(1+(y/x)*(y/x)));
return (y*sqrt(1+(y/x)*(y/x)));
}


void CComplexCaculatorDlg::OnPlus()
{
// TODO: Add your control notification handler code here
UpdateData();
CComplex cpxA(double m_dblA,double m_dblB);
CComplex cpxB(double m_dblC,double m_dblD);
CComplex cpxC= cpxA*cpxB;
m_strResult=cpxC.ToString();
UpdateData(FALSE);
}
--------------------Configuration: ComplexCaculator - Win32 Debug--------------------
Compiling...
ComplexCaculatorDlg.cpp
D:\c++\CComplex\ComplexCaculatorDlg.cpp(199) : error C2296: '*' : illegal, left operand has type 'class CComplex (__cdecl *)(double,double)'
D:\c++\CComplex\ComplexCaculatorDlg.cpp(199) : error C2297: '*' : illegal, right operand has type 'class CComplex (__cdecl *)(double,double)'
D:\c++\CComplex\ComplexCaculatorDlg.cpp(242) : error C2228: left of '.Abs' must have class/struct/union type
Error executing cl.exe.
Creating browse info file...
ComplexCaculator.exe - 3 error(s), 0 warning(s)
您需要登录后才可以回帖 登录 | 注册

本版积分规则

申请友链|小黑屋|最新主题|手机版|新微赢技术网 ( 苏ICP备08020429号 )  

GMT+8, 2024-11-18 19:42 , Processed in 0.170950 second(s), 10 queries , Gzip On, Memcache On.

Powered by xuexi

© 2001-2013 HaiAn.Com.Cn Inc. 寰耽

快速回复 返回顶部 返回列表