学习过C++教材后,自己动手写了一个复数类模板,它可以处理都中数据类型的加减乘除,并且重载了输出流,在编程过程中遇到了许多问题,思路是没有任何问题,就是对模板的使用技巧不生了解,编写的代码如下:
#ifndef COMPLEX_H
#define COMPLEX_H
#include<iostream>
using std::ostream;
template < class T > class complex;
template < class T > ostream &operator<<( ostream &output, const complex<T> &operand );
/*这一声明可以不要*/
template <class T> class complex
{ template < class TT > friend ostream &operator<<( ostream &output,
const complex< TT > &operand );
public:
complex( T , T );
complex<T> operator+( const complex<T> & ) const;
complex<T> operator-( const complex<T> & ) const;
complex<T> operator*( const complex<T> & ) const;
bool operator==( const complex<T> & ) const;
bool operator!=( const complex<T> & ) const;
// void Print() const;
private:
T real;
T imaginary;
};
#endif
#include<iostream>
using std::cout;
using std::endl;
//#inlcude "complex.h"
template < class T > complex<T>::complex( T realprt, T imaginaryprt )
:real( realprt ),imaginary( imaginaryprt )
{
}
template < class T > complex<T> complex< T >::operator+( const complex<T> &operand ) const
{ return complex( real + operand.real, imaginary + operand.imaginary );
}
template < class T > complex<T> complex< T >::operator-( const complex<T> &operand ) const
{ return complex( real - operand.real, imaginary - operand.imaginary );
}
template < class T > complex<T> complex< T >::operator*( const complex<T> &operand ) const
{ return complex( real*operand.real - imaginary*operand.imaginary,
real*operand.imaginary + imaginary*operand.real );
}
template < class T > bool complex< T >::operator==( const complex<T> &operand ) const
{ if( real == operand.real && imaginary == operand.imaginary )
return true;
else
return false;
}
template < class T > bool complex< T >::operator!=( const complex<T> &operand ) const
{ if( real != operand.real || imaginary != operand.imaginary )
return true;
else
return false;
}
/* template < class T >
complex< T >::Print() const
{ cout<<"( "<<real<<","<<imaginary<<" )";
} */
//template< class T >
template < class T T> ostream &operator<<( ostream &output, const complex<TT> &operand )
{ output<<"( "<<operand.real<<","<<operand.imaginary<<" )";
return output;
}
//difinition of main
#include<iostream>
using std::cout;
using std::cin;
using std::endl;
//include "complex.h"
int main()
{ complex< int > x1( 4, 5 );
complex< int > x2( 8, 9 );
complex< int > x3( 4, 5 );
complex< double > y1(2.2,3.3);
complex< double > y2(4.5,8.5);
complex< double > y3(2.2,3.3);
cout<<"x1 + x2 = "<<(x1 + x2)<<endl;
cout<<"y1 + y2 = "<<(y1 + y2 )<<endl;
cout<<"x1 * x2 = "<<(x1 * x2 )<<endl;
cout<<"y1 * y2 = "<<(y1 * y2 )<<endl;
// cout<<"x1 * y1 = "<<(x1 * y1 )<<endl;
if( x1 == x2 )
cout<<"x1 and x2 are equal/n";
else
cout<<"x1 and x2 are unequal/n";
if(x2 != x3 )
cout<<"x2 and x3 are not equal/n";
if(y1 == y3 )
cout<<"y1 and y3 are equal/n";
if(y2 != y3 )
cout<<"y2 and y3 are not equal/n";
system("pause");
return 0;
}
编写过程中,如果把
template < class TT > friend ostream &operator<<( ostream &output,
const complex< TT > &operand )
写成===》
template < class T > friend ostream &operator<<( ostream &output,
const complex< T > &operand );则编译时出现模板重复声明的情况,查了好多资料,最后才再CSDN上看到了解决的办法。