C++类模板,重载-函数没有用,怎么修改
#include<iostream>using namespace std;
template<class T>
class Complex
{
private:
T real;
T imag;
public:
Complex<T>(T r,T i);
Complex<T>();
void print();
Complex<T> operator+(Complex<T> &c);
Complex<T> operator-(Complex<T> &c);
};
template<class T>
Complex<T>::Complex<T>(T r,T i)
{
real=r;
imag=i;
}
template<class T>
Complex<T>::Complex<T>()
{
real=0;
imag=0;
}
template<class T>
void Complex<T>::print()
{
cout<<real;
if(imag!=0)
{
if(imag>0) cout<<"+";
cout<<imag<<"i";
}
else cout<<imag<<"i";
cout<<endl;
}
template<class T>
Complex<T> Complex<T>::operator +(Complex<T> &c)
{
real+=c.real;
imag+=c.imag;
return *this;
}
template<class T>
Complex<T> Complex<T>::operator -(Complex <T> &c)
{
real-=c.real;
imag-=c.imag;
return *this;
}
int main()
{
Complex<int> c1(1,2),c2(4,5),c5,c6;
Complex<double> c3(2.4,4.5),c4(3.3,6.6),c7,c8;
c5=c1+c2;
c6=c1-c2;
c7=c3+c4;
c8=c3-c4;
c5.print();
c6.print();
c7.print ();
c8.print();
return 0;
}