在C++环境中使用类模板出现的问题-请大家解决一下
下面的程序在C++6.0环境运行时有五个错误,都发生在成员函数定义上,请大家帮忙解决一下,雷某在这里先谢谢大家了!
#include <iostream>
using namespace std;
template <class numtype>
class Complex
{
public:
Complex( ){real=0;imag=0;}
Complex(numtype r,numtype i){real=r;imag=i;}
Complex complex_add(Complex &c2);
Complex complex_jian(Complex &c2);
Complex complex_cheng(Complex &c2);
Complex complex_chu(Complex &c2);
void display( );
private:
numtype real;
numtype imag;
};
template <class numtype>
Complex Complex<numtype>::complex_add(Complex &c2)
{
Complex c;
c.real=real+c2.real;
c.imag=imag+c2.imag;
return c;
}
template <class numtype>
Complex Complex<numtype>::complex_jian(Complex &c2)
{
Complex c;
c.real=real-c2.real;
c.imag=imag-c2.imag;
return c;
}
template <class numtype>
Complex Complex<numtype>::complex_cheng(Complex &c2)
{
Complex c;
c.real=real*c2.real-imag*c2.imag;
c.imag=real*c2.imag+imag*c2.real;
return c;
}
template <class numtype>
Complex Complex<numtype>::complex_chu(Complex &c2)
{
Complex c,c3,c4;
c3.real=c2.real;
c3.imag=c2.imag;
c.real=real*c3.real-imag*c3.imag;
c.imag=real*c3.imag+imag*c3.real;
c4.real=c2.real*c2.real-c2.imag*c3.imag;
c.real=c.real/c4.real;
c.imag=c.image/c4.real;
return c;
}
template <class numtype>
void Complex<numtype>::display( )
{
cout<<"("<<real<<","<<imag<<"i)"<<endl;
}
int main( )
{
Complex<int> c1(3,4),c2(5,-10),c3;
c3=c1.complex_add(c2);
cout<<"c1+c2=";
c3.display( );
Complex<double> c4(3.1,4.4),c5(5.34,-10.21),c6;
c6=c4.complex_jian(c5);
cout<<"c4+c5=";
c6.display( );
Complex<int> c7(3.1,4.4),c8(5.34,-10.21),c9;
c9=c7.complex_cheng(c8);
cout<<"c7*c8=";
c9.display( );
Complex<double> c10(3.1,4.4),c11(5.34,-10.21),c12;
c12=c10.complex_chu(c11);
cout<<"c10/c11=";
c12.display( );
system("pause");
return 0;
}
[解决办法]
你都没定义赋值构造函数
Complex Complex<numtype>::complex_add(Complex &c2)
{
Complex c;
c.real=real+c2.real;
c.imag=imag+c2.imag;
return c;
}
这个定义肯定错了。
[解决办法]
Complex Complex<numtype>::complex_jian(Complex &c2)
{
Complex c;
c.real=real-c2.real;
c.imag=imag-c2.imag;
return c;
}
template <class numtype>
Complex Complex<numtype>::complex_cheng(Complex &c2)
{
Complex c;
c.real=real*c2.real-imag*c2.imag;
c.imag=real*c2.imag+imag*c2.real;
return c;
}
template <class numtype>
Complex Complex<numtype>::complex_chu(Complex &c2)
{
Complex c,c3,c4;
c3.real=c2.real;
c3.imag=c2.imag;
c.real=real*c3.real-imag*c3.imag;
c.imag=real*c3.imag+imag*c3.real;
c4.real=c2.real*c2.real-c2.imag*c3.imag;
c.real=c.real/c4.real;
c.imag=c.image/c4.real;
return c;
}
这几个都有问题
[解决办法]
template <class numtype>class Complex {public:Complex( ){real=0;imag=0;} Complex(numtype r,numtype i){real=r;imag=i;} Complex complex_add(Complex &c2); Complex complex_jian(Complex &c2); Complex complex_cheng(Complex &c2); Complex complex_chu(Complex &c2); void display( ); private:numtype real; numtype imag; };template <class numtype>Complex<numtype> Complex<numtype>::complex_add(Complex &c2){Complex<numtype> c;c.real=real+c2.real;c.imag=imag+c2.imag;return c;} template <class numtype>Complex<numtype> Complex<numtype>::complex_jian(Complex &c2){Complex c;c.real=real-c2.real;c.imag=imag-c2.imag;return c;}template <class numtype>Complex<numtype> Complex<numtype>::complex_cheng(Complex &c2){Complex c;c.real=real*c2.real-imag*c2.imag;c.imag=real*c2.imag+imag*c2.real;return c;} template <class numtype>Complex<numtype> Complex<numtype>::complex_chu(Complex &c2){Complex<numtype> c,c3,c4;c3.real=c2.real;c3.imag=c2.imag;c.real=real*c3.real-imag*c3.imag;c.imag=real*c3.imag+imag*c3.real;c4.real=c2.real*c2.real-c2.imag*c3.imag;c.real=c.real/c4.real;c.imag=c.imag/c4.real;return c;} template <class numtype> void Complex<numtype>::display( ) {cout<<"("<<real<<","<<imag<<"i)"<<endl;}int main( ){ Complex<int> c1(3,4);Complex<int>c2(5,-10);Complex<int>c3; c3=c1.complex_add(c2); cout<<"c1+c2="; c3.display( ); Complex<double> c4(3.1,4.4);Complex<double>c5(5.34,-10.21);Complex<double>c6; c6=c4.complex_jian(c5); cout<<"c4+c5="; c6.display( ); Complex<double> c7(3.1,4.4);Complex<double> c8(5.34,-10.21);Complex<double> c9; c9=c7.complex_cheng(c8); cout<<"c7*c8="; c9.display( ); Complex<double> c10(3.1,4.4);Complex<double> c11(5.34,-10.21);Complex<double> c12; c12=c10.complex_chu(c11); cout<<"c10/c11="; c12.display( ); system("pause"); return 0;}
[解决办法]
template <class numtype>Complex<numtype> Complex<numtype>::complex_add(const Complex<numtype> &c2){Complex<numtype> c;c.real=real+c2.real;c.imag=imag+c2.imag;return c;}
[解决办法]
#include <iostream>using namespace std;template <class numtype>class Complex {public: Complex( ){real=0;imag=0;} Complex(numtype r,numtype i){real=r;imag=i;} void operator = (Complex c2);//赋值 Complex<numtype> complex_add(Complex &c2); Complex<numtype> complex_jian(Complex &c2); Complex<numtype> complex_cheng(Complex &c2); Complex<numtype> complex_chu(Complex &c2); void display( ); private: numtype real; numtype imag; };template <class numtype>void Complex<numtype>::operator = (Complex c2){ real=c2.real; imag=c2.imag;} template <class numtype>Complex<numtype> Complex<numtype>::complex_add(Complex &c2){ real+=c2.real; imag+=c2.imag; return *this;} template <class numtype>Complex<numtype> Complex<numtype>::complex_jian(Complex &c2){ Complex c; c.real=real-c2.real; c.imag=imag-c2.imag; return c;}template <class numtype>Complex<numtype> Complex<numtype>::complex_cheng(Complex &c2){ Complex c; c.real=real*c2.real-imag*c2.imag; c.imag=real*c2.imag+imag*c2.real; return c;} template <class numtype>Complex<numtype> Complex<numtype>::complex_chu(Complex &c2){ Complex c,c3,c4; c3.real=c2.real; c3.imag=c2.imag; c.real=real*c3.real-imag*c3.imag; c.imag=real*c3.imag+imag*c3.real; c4.real=c2.real*c2.real-c2.imag*c3.imag; c.real=c.real/c4.real; c.imag=c.imag/c4.real; return c;} template <class numtype> void Complex<numtype>::display( ) { cout<<"("<<real<<","<<imag<<"i)"<<endl;}int main( ){ Complex<int> c1(3,4),c2(5,-10),c3; c3=c1.complex_add(c2); cout<<"c1+c2="; c3.display( ); Complex<double> c4(3.1,4.4),c5(5.34,-10.21),c6; c6=c4.complex_jian(c5); cout<<"c4+c5="; c6.display( ); Complex<int> c7(3.1,4.4),c8(5.34,-10.21),c9; c9=c7.complex_cheng(c8); cout<<"c7*c8="; c9.display( ); Complex<double> c10(3.1,4.4),c11(5.34,-10.21),c12; c12=c10.complex_chu(c11); cout<<"c10/c11="; c12.display( ); system("pause"); return 0;}