对于使用类模板下的友员函数的使用注意
来源:互联网 发布: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
- 对于使用类模板下的友员函数的使用注意
- 对于switch使用的注意
- 对于通配符*的使用要注意
- 关于操作符的友员模板函数重载--无法链接友员函数
- 使用函数模板需要注意的几个地方
- 函数模板和类模板的使用
- 模板函数以及模板类的使用
- 函数模板和类模板的使用
- 类模板和函数模板的使用
- 模板类中的友元函数使用注意
- 函数模板的使用
- 模板函数的使用
- 模板函数的使用
- 函数模板的使用
- 函数:模板的使用
- 使用模板库的几个注意点
- 关于_RecordsetPtr下GetRecordCount函数的使用注意
- 对于使用xml配置文档时应该注意的问题
- java compiler level does not match the version of the installed java project原因系类
- HDU4978概率几何
- lifery ActionURL和RenderURL区别
- java.jse-线程-线程池-java自带线程池和队列详细讲解
- mysql 语句大全
- 对于使用类模板下的友员函数的使用注意
- Android inline hook
- ios TableView那些事2(Swift 一) 初见TableView
- http请求
- eclipse配置nodejs开发环境
- Android工程的目录结构详解
- 11.26 每天总结工作,一天胜似一周
- Eclipse + keil 使用教程
- 虚拟机中的redhat linux网络配置与主机通信