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

来源:互联网 发布:350淘宝模板破解 编辑:程序博客网 时间:2024/06/07 23:09
今天编了一个使用模板的数组
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_iteratortemplate<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而之后一句则是用来对输出流重载时的一种模板下的定义。毕竟,不能再类内部定义模板函数。!!!!长知识了大笑
                                             
0 0