求教高手,complex模板如何转成用声明对象的做法

DENGGUOGANG 2006-09-28 06:34:47
程序如下:
HPP
#include<iostream.h>
#include <cmath>
// class
class complex
{
private:

double a,b;
double _copy;


public:
double getreal();
double getimag();
double getZ();
complex(int x)
{
a=x;b=0;
}
complex(double av,double bv):a(av),b(bv)
{
}
complex(){}
complex(complex& _copy)
{
a=_copy.a;
b=_copy.b;
}

friend ostream& operator<<(ostream& i,complex& j);
friend istream& operator>>(istream& g,complex& i);


friend complex operator+(complex& x,complex& y);
friend complex operator-(complex& x,complex& y);
friend complex operator*(complex& x,complex& y);
friend complex operator/(complex& x,complex& y);
complex operator=(complex& y);
void output()
{
cout<<"z="<<sqrt((a*a)+(b*b))<<endl;
}
void put()
{
cout<<"("<<a<<"+"<<b<<"i"<<")"<<endl;
}
void gen()
{int sign=1, double g,h;
if(b<0)
sign=-1;
else
sign=1;
g=sqrt((sqrt((a*a)+(b*b))+a)/2);
h=sign*sqrt((sqrt((a*a)+(b*b))-a)/2);
cout<<"the sqrt-root is: "<<g<<"+"<<h<<"i"<<endl;
}
};
ostream& operator<<(ostream& stream ,complex& x)
{
x.put();
return stream;
}

//input
istream& operator>>(istream& stream,complex& x)
{
cout<<"\treal=";
cin>>x.a;
cout<<"\timag=";
cin>>x.b;
return stream;
}
//add
complex operator+(complex& x,complex& y)
{
return complex(x.a+y.a,x.b+y.b);
}
//sub
complex operator-(complex& x,complex& y)
{
return complex(x.a-y.a,x.b-y.b);
}
//mul
complex operator*(complex& x,complex &y)
{
return complex((x.a*y.a)-(x.b*y.b),(x.a*y.b)+(x.b*y.a));
}
//div
complex operator/(complex& x,complex& y)
{
return complex(((x.a*y.a+x.b*y.b))/(y.a*y.a+y.b*y.b),(x.b*y.a-x.a*y.b)/(y.a*y.a+y.b*y.b));
}

//assignment
complex complex::operator=(complex& y)
{
this->a=y.a;
this->b=y.b;
return *this;
}
double complex::getreal()
{
return a;
}
double complex::getimag()
{
return b;
}
double complex::getZ()
{
return sqrt(a*a+b*b);
}



CPP
#include <iostream>
#include <complex>
#include <cmath>
using namespace std;

int main(void)
{
double a ,b ,c ;
cout<<"input a b c:";
cin>>a>>b>>c;
complex<double> x1,x2;
double d = b * b - 4.0 * a * c;
if (d >= 0)
{
x1 = (-b + sqrt(d))/(2*a);
x2 = (-b - sqrt(d))/(2*a);
}
else
{
x1 = complex<double>((-b)/(2*a), sqrt(-d)/(2*a));
x2 = complex<double>((-b)/(2*a), -(sqrt(-d)/(2*a)));
}
cout << "x1 = " << x1.real() << " + " << x1.imag() << "i" << endl;
cout << "x2 = " << x2.real() << " + " << x2.imag() << "i" << endl;

return 0;
}
请高手帮我改一下
...全文
432 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
飞哥 2006-09-29
  • 打赏
  • 举报
回复
template <class T>//加一句,,然后将你用到的数据类型complex<double>,就是double,改成T
class complex
{
private:

T a,b;
T _copy;

sinall 2006-09-29
  • 打赏
  • 举报
回复
#include <iostream>
// #include <complex>
#include <cmath>
using namespace std;

……

int main(void)
{
double a ,b ,c ;
cout<<"input a b c:";
cin>>a>>b>>c;
::complex x1,x2;
double d = b * b - 4.0 * a * c;
if (d >= 0)
{
x1 = (-b + sqrt(d))/(2.0*a);
x2 = (-b - sqrt(d))/(2.0*a);
}
else
{
x1 = ::complex((-b)/(2.0*a), sqrt(-d)/(2.0*a));
x2 = ::complex((-b)/(2.0*a), -(sqrt(-d)/(2.0*a)));
}
cout << "x1 = " << x1.getreal() << " + " << x1.getimag() << "i" << endl;
cout << "x2 = " << x2.getreal() << " + " << x2.getimag() << "i" << endl;

return 0;
}

cnhgj 2006-09-29
  • 打赏
  • 举报
回复
class complex //不是模板类,不能用complex<double>
weijiangshanwww 2006-09-29
  • 打赏
  • 举报
回复
但是要把以下这部分另外作为一个头文件使用,就可以了。
#include<iostream.h>
#include <cmath>
// class
class complex
{
private:

double a,b;
double _copy;


public:
double getreal();
double getimag();
double getZ();
complex(int x)
{
a=x;b=0;
}
complex(double av,double bv):a(av),b(bv)
{
}
complex(){}
complex(complex& _copy)
{
a=_copy.a;
b=_copy.b;
}

friend ostream& operator<<(ostream& i,complex& j);
friend istream& operator>>(istream& g,complex& i);


friend complex operator+(complex& x,complex& y);
friend complex operator-(complex& x,complex& y);
friend complex operator*(complex& x,complex& y);
friend complex operator/(complex& x,complex& y);
complex operator=(complex& y);
void output()
{
cout<<"z="<<sqrt((a*a)+(b*b))<<endl;
}
void put()
{
cout<<"("<<a<<"+"<<b<<"i"<<")"<<endl;
}
void gen()
{
int sign=1;
double g,h;
if(b<0)
sign=-1;
else
sign=1;
g=sqrt((sqrt((a*a)+(b*b))+a)/2);
h=sign*sqrt((sqrt((a*a)+(b*b))-a)/2);
cout<<"the sqrt-root is: "<<g<<"+"<<h<<"i"<<endl;
}
};
ostream& operator<<(ostream& stream ,complex& x)
{
x.put();
return stream;
}

//input
istream& operator>>(istream& stream,complex& x)
{
cout<<"\treal=";
cin>>x.a;
cout<<"\timag=";
cin>>x.b;
return stream;
}
//add
complex operator+(complex& x,complex& y)
{
return complex(x.a+y.a,x.b+y.b);
}
//sub
complex operator-(complex& x,complex& y)
{
return complex(x.a-y.a,x.b-y.b);
}
//mul
complex operator*(complex& x,complex &y)
{
return complex((x.a*y.a)-(x.b*y.b),(x.a*y.b)+(x.b*y.a));
}
//div
complex operator/(complex& x,complex& y)
{
return complex(((x.a*y.a+x.b*y.b))/(y.a*y.a+y.b*y.b),(x.b*y.a-x.a*y.b)/(y.a*y.a+y.b*y.b));
}

//assignment
complex complex::operator=(complex& y)
{
this->a=y.a;
this->b=y.b;
return *this;
}
double complex::getreal()
{
return a;
}
double complex::getimag()
{
return b;
}
double complex::getZ()
{
return sqrt(a*a+b*b);
}
weijiangshanwww 2006-09-29
  • 打赏
  • 举报
回复
改好了,可以编译了。
#include<iostream.h>
#include <cmath>
// class
class complex
{
private:

double a,b;
double _copy;


public:
double getreal();
double getimag();
double getZ();
complex(int x)
{
a=x;b=0;
}
complex(double av,double bv):a(av),b(bv)
{
}
complex(){}
complex(complex& _copy)
{
a=_copy.a;
b=_copy.b;
}

friend ostream& operator<<(ostream& i,complex& j);
friend istream& operator>>(istream& g,complex& i);


friend complex operator+(complex& x,complex& y);
friend complex operator-(complex& x,complex& y);
friend complex operator*(complex& x,complex& y);
friend complex operator/(complex& x,complex& y);
complex operator=(complex& y);
void output()
{
cout<<"z="<<sqrt((a*a)+(b*b))<<endl;
}
void put()
{
cout<<"("<<a<<"+"<<b<<"i"<<")"<<endl;
}
void gen()
{
int sign=1;
double g,h;
if(b<0)
sign=-1;
else
sign=1;
g=sqrt((sqrt((a*a)+(b*b))+a)/2);
h=sign*sqrt((sqrt((a*a)+(b*b))-a)/2);
cout<<"the sqrt-root is: "<<g<<"+"<<h<<"i"<<endl;
}
};
ostream& operator<<(ostream& stream ,complex& x)
{
x.put();
return stream;
}

//input
istream& operator>>(istream& stream,complex& x)
{
cout<<"\treal=";
cin>>x.a;
cout<<"\timag=";
cin>>x.b;
return stream;
}
//add
complex operator+(complex& x,complex& y)
{
return complex(x.a+y.a,x.b+y.b);
}
//sub
complex operator-(complex& x,complex& y)
{
return complex(x.a-y.a,x.b-y.b);
}
//mul
complex operator*(complex& x,complex &y)
{
return complex((x.a*y.a)-(x.b*y.b),(x.a*y.b)+(x.b*y.a));
}
//div
complex operator/(complex& x,complex& y)
{
return complex(((x.a*y.a+x.b*y.b))/(y.a*y.a+y.b*y.b),(x.b*y.a-x.a*y.b)/(y.a*y.a+y.b*y.b));
}

//assignment
complex complex::operator=(complex& y)
{
this->a=y.a;
this->b=y.b;
return *this;
}
double complex::getreal()
{
return a;
}
double complex::getimag()
{
return b;
}
double complex::getZ()
{
return sqrt(a*a+b*b);
}

#include <iostream>
#include <complex>
#include <cmath>
using namespace std;

int main(void)
{
double a ,b ,c ;
cout<<"input a b c:";
cin>>a>>b>>c;
complex<double> x1,x2;
double d = b * b - 4.0 * a * c;
if (d >= 0)
{
x1 = (-b + sqrt(d))/(2*a);
x2 = (-b - sqrt(d))/(2*a);
}
else
{
x1 = complex<double>((-b)/(2*a), sqrt(-d)/(2*a));
x2 = complex<double>((-b)/(2*a), -(sqrt(-d)/(2*a)));
}
cout << "x1 = " << x1.real() << " + " << x1.imag() << "i" << endl;
cout << "x2 = " << x2.real() << " + " << x2.imag() << "i" << endl;

return 0;
}
OOPhaisky 2006-09-29
  • 打赏
  • 举报
回复
楼主的complex类和标准库中的complex类模版重名了!
楼主换一个名字(比如mycomplex),或者不要包含<complex>。
suhanzhong 2006-09-28
  • 打赏
  • 举报
回复
你的类是不是和标准库的 类同名了
complex
DENGGUOGANG 2006-09-28
  • 打赏
  • 举报
回复
回三楼有错呀大哥
--------------------Configuration: 11111 - Win32 Debug--------------------
Compiling...
11111.cpp
D:\C++\MSDev98\MyProjects\1111\11111.cpp(11) : error C2955: 'complex' : use of class template requires template argument list
d:\microsoft visual studio\vc98\include\complex(267) : see declaration of 'complex'
D:\C++\MSDev98\MyProjects\1111\11111.cpp(11) : error C2133: 'x1' : unknown size
D:\C++\MSDev98\MyProjects\1111\11111.cpp(11) : error C2512: 'complex' : no appropriate default constructor available
D:\C++\MSDev98\MyProjects\1111\11111.cpp(11) : error C2262: 'x1' : cannot be destroyed
D:\C++\MSDev98\MyProjects\1111\11111.cpp(11) : error C2955: 'complex' : use of class template requires template argument list
d:\microsoft visual studio\vc98\include\complex(267) : see declaration of 'complex'
D:\C++\MSDev98\MyProjects\1111\11111.cpp(11) : error C2133: 'x2' : unknown size
D:\C++\MSDev98\MyProjects\1111\11111.cpp(11) : error C2512: 'complex' : no appropriate default constructor available
D:\C++\MSDev98\MyProjects\1111\11111.cpp(11) : error C2262: 'x2' : cannot be destroyed
D:\C++\MSDev98\MyProjects\1111\11111.cpp(15) : error C2678: binary '=' : no operator defined which takes a left-hand operand of type 'class std::complex' (or there is no acceptable conversion)
D:\C++\MSDev98\MyProjects\1111\11111.cpp(15) : error C2582: 'std::complex' : 'operator =' function is unavailable
D:\C++\MSDev98\MyProjects\1111\11111.cpp(16) : error C2678: binary '=' : no operator defined which takes a left-hand operand of type 'class std::complex' (or there is no acceptable conversion)
D:\C++\MSDev98\MyProjects\1111\11111.cpp(16) : error C2582: 'std::complex' : 'operator =' function is unavailable
D:\C++\MSDev98\MyProjects\1111\11111.cpp(20) : error C2955: 'complex' : use of class template requires template argument list
d:\microsoft visual studio\vc98\include\complex(267) : see declaration of 'complex'
D:\C++\MSDev98\MyProjects\1111\11111.cpp(21) : error C2955: 'complex' : use of class template requires template argument list
d:\microsoft visual studio\vc98\include\complex(267) : see declaration of 'complex'
D:\C++\MSDev98\MyProjects\1111\11111.cpp(23) : error C2039: 'getreal' : is not a member of 'complex'
d:\microsoft visual studio\vc98\include\complex(267) : see declaration of 'complex'
D:\C++\MSDev98\MyProjects\1111\11111.cpp(23) : error C2039: 'getimag' : is not a member of 'complex'
d:\microsoft visual studio\vc98\include\complex(267) : see declaration of 'complex'
D:\C++\MSDev98\MyProjects\1111\11111.cpp(24) : error C2039: 'getreal' : is not a member of 'complex'
d:\microsoft visual studio\vc98\include\complex(267) : see declaration of 'complex'
D:\C++\MSDev98\MyProjects\1111\11111.cpp(24) : error C2039: 'getimag' : is not a member of 'complex'
d:\microsoft visual studio\vc98\include\complex(267) : see declaration of 'complex'
执行 cl.exe 时出错.

11111.obj - 1 error(s), 0 warning(s)
飞哥 2006-09-28
  • 打赏
  • 举报
回复
#include<iostream.h>
#include <cmath>
呵呵,怎么感觉有点不伦不类呢

#include<complex>
#include<iostream>
using namespace std;
你可以使STL试试

sinall 2006-09-28
  • 打赏
  • 举报
回复
int main(void)
{
double a ,b ,c ;
cout<<"input a b c:";
cin>>a>>b>>c;
complex x1,x2;
double d = b * b - 4.0 * a * c;
if (d >= 0)
{
x1 = (-b + sqrt(d))/(2.0*a);
x2 = (-b - sqrt(d))/(2.0*a);
}
else
{
x1 = complex((-b)/(2.0*a), sqrt(-d)/(2.0*a));
x2 = complex((-b)/(2.0*a), -(sqrt(-d)/(2.0*a)));
}
cout << "x1 = " << x1.getreal() << " + " << x1.getimag() << "i" << endl;
cout << "x2 = " << x2.getreal() << " + " << x2.getimag() << "i" << endl;

return 0;
}
suhanzhong 2006-09-28
  • 打赏
  • 举报
回复
complex<double> x1,x2; //此句有错,你这个是普通类,不是模板类 所以错了!!!
应该该为
complex x1,x2; //
看一下书 你就应该明白的了

64,654

社区成员

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

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