对于使用类模板下的友员函数的使用注意

今天编了一个使用模板的数组
using namespace std;
//class template Array
<span style="color:#3333ff;">template<typename T>
class Array;
template<typename T>ostream & operator <<(ostream & out,const Array<T>&aa);</span>
template<typename T>
class Array{
public:
    typedef typename list<T>::iterator  iter;
    typedef typename list<T>::const_iterator const_iter;
    Array(){}//constructor
    Array(T array[],T count) {
        for (T i = 0; i < count; ++i) {
            MyArray.push_back( array [ i ] );
        }  MyArray.unique( );
    }//constructor with index
    Array(const Array<T>& aa ) {
        if ( &aa == this ) return;
        MyArray.clear( );
        MyArray = aa.MyArray;
    }//copy constructor
    //function begin for iterator
    iter begin(){
        return MyArray.begin();
    }
    //function end for iterator
    iter end(){
        return MyArray.end();
    }
    //function begin for const_iterator
    const_iter begin()const{
        return ((const list<T>&)MyArray).begin();
    }
     //function end for const_iterator
    const_iter end()const{
        return ((const list<T>&)MyArray).end();
    }
    //overloading operator =
    Array<T>& operator =(const Array<T>& aa){
        if(&aa==this)return *this;
        MyArray.clear();
        MyArray=aa.MyArray;
        return *this;
    }
 <span style="color:#ff0000;">  friend ostream & operator <<(ostream & out,const Array<T>&aa)</span>{
        out<<"[";
         const_iter it=aa.begin();
        if(it!=aa.end()){
            out<<*it;

        if(it!=aa.end()){
            for(it++;it!=aa.end();it++)
            out<<","<<*it;
        }
        }
        out<<"]";
        return out;
    }

private:

    list<T>  MyArray;//container named MyArray

};
//Defined a class named A_iterator
template<typename T>
class A_iterator{
public:

    A_iterator(){
    }//constructor

    A_iterator(typename list<T>::iterator it){
        this->it = it;
    }//construtor with index

   typename  list<T>::iterator getIt() const{
        return it;
    }//function getIt

    T& operator*(){
        return *it;
    }//overloading operator *

    typename list<T>::iterator& operator++(){
        ++it;
        return it;
    }//overloading operator ++
   typename list<T>::iterator  operator++(int){
        list<int>::iterator old = it;
        it++;
        return old;
    }//overloding operator ++ in back sequnce
    bool operator!=(const A_iterator& b){
        return it!=b.getIt();
    }//overloading operator !=
    bool operator==(const A_iterator& b){
        return it==b.getIt();
    }//overloding operator ==

private:
 typename list<T>::iterator it;//container_iterator named MyArray
};
之前在
<span style="color: rgb(255, 0, 0);">friend ostream & operator <<(ostream & out,const Array<T>&aa)</span>
<span style="color: rgb(255, 0, 0);">这一句运行时老是出错,其解决的方式在于加上类似蓝色部分</span>
<span style="color: rgb(255, 0, 0);"></span><pre name="code" class="html"><span style="color: rgb(51, 51, 255);">template<typename T>
class Array;
template<typename T>ostream & operator <<(ostream & out,const Array<T>&aa);</span>
其中第一句是用来声明使用这个类型Array而之后一句则是用来对输出流重载时的一种模板下的定义。毕竟,不能再类内部定义模板函数。!!!!长知识了 大笑

                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值