类模板与输出函数的重载

 学习过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上看到了解决的办法。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zxhyxwwu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值