| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 616 人关注过本帖
标题:运算符重载错误
只看楼主 加入收藏
dxk331huda
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2006-9-17
收藏
 问题点数:0 回复次数:0 
运算符重载错误

#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)

搜索更多相关主题的帖子: 运算符 重载 
2006-10-11 14:03
快速回复:运算符重载错误
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.011876 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved