#include<iostream.h> #include<stdlib.h> class CPoint { private: int x,y; friend ostream & operator << (ostream &r_out,const CPoint &r_p); public: CPoint(){x=0;y=0;} CPoint(const CPoint &r_p){ x=r_p.x; y=r_p.y; } void set(int a,int b) { x=a; y=b; } };
ostream & operator <<(ostream &r_out,const CPoint &r_p) { r_out<<"("<<r_p.x<<","<<r_p.y<<")"; return r_out; }
template<class T> class CStack { private: T *p_items; int top,size; CStack(const CStack &); operator = (const CStack &); public: CStack(int); void push(const T&); T pop(); bool isEmpty() const; ~CStack(); };
template<class T> CStack<T>::CStack(int sz) { size=sz; top=0; p_items=new T[size]; if(p_items==NULL) { cout<<"Out of memory\n"; exit(1); } }
template< class T1> CStack<T1>::push(const T1 &r_c) { if(top<size) p_items[top++]=r_c; else { T1 *p=new T1[size*2]; if(p==NULL) { cout<<"Out of memory\n"; exit(1); } for(int i=0;i<size;i++) p[i]=p_items[i]; delete [] p_item; p_items=p; size*=2; cout<<"\nNew size:"<<size; p_items[top++]=r_c; } }//编译是出现这样错误: error C2244: 'CStack<T>::push' : unable to resolve function overload
template<class T> //编译时出现:error C2954: template definitions cannot nest CStack<T>::pop() { return p_items[--top]; }
template<class Type> bool CStack<Type>::isEmpty() const { return top==0; }
template<class T> CStack<T>::~CStack() { delete [] p_item; }
void main() { int int_data[5]={1,3,5,7,9},i; CStack<int> int_stack(4); cout<<"Initial data:"; for(i=0;i<5;i++) { cout<<int_data[i]; int_stack.push(int_data[i]); }
cout<<"\nInitial data:"; while(!int_stack.isEmpty()) cout<<int_stack.pop()<<" "; cout<<endl<<endl; CPoint point_data[5]; CStack<CPoint> point_stack(5); point_data[0].set(1,2); point_data[1].set(3,4); point_data[2].set(5,6); point_data[3].set(7,8); point_data[4].set(9,10); point_data[5].set(11,12); cout<<"Initial data:"; for(i=0;i<5;i++) { cout<<point_data[i]<<" "; point_stack.push(point_data[i]); } cout<<"\nInitial data:"; while(!point_stack.isEmpty()) { cout<<point_stack.pop()<<" "; cout<<endl; } }
错误行我已有注释,大家帮忙看下到底是什么错误,小弟实在是找不出错误