| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1158 人关注过本帖
标题:[求助]模版栈的问题
只看楼主 加入收藏
骇客
Rank: 1
等 级:新手上路
帖 子:25
专家分:0
注 册:2004-11-9
收藏
 问题点数:0 回复次数:3 
[求助]模版栈的问题

#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; } }

错误行我已有注释,大家帮忙看下到底是什么错误,小弟实在是找不出错误

搜索更多相关主题的帖子: 模版 
2004-11-25 22:27
kai
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:52
帖 子:3450
专家分:59
注 册:2004-4-25
收藏
得分:0 

#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> void CStack<T1>::push(const T1 &r_c) // you must add at begin "void" { 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_items; // here is also a type error p_items=p; size*=2; cout<<"\nNew size:"<<size; p_items[top++]=r_c; } }

template<class T> T CStack<T>::pop() // here is the same error, you should add T at begin { return p_items[--top]; }

template<class Type> bool CStack<Type>::isEmpty() const { return top==0; }

template<class T> CStack<T>::~CStack() { delete [] p_items; // type error }

int main() // please don't defined it as "void" { int int_data[5]={1,3,5,7,9}; int i = 0; 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); // program in such way to write in very dangerous. // you should alway pay attention, that manipulation not over its place in RAM 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); // here is an error, you have not this 5th element 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; } return 0; }

// viel Spass !!!


自由,民主,平等,博爱,进步.
中华民国,我的祖国,中华民国万岁!中华民国加油!
本人自愿加入中国国民党,为人的自由性,独立性和平等性而奋斗!
2004-11-26 03:55
骇客
Rank: 1
等 级:新手上路
帖 子:25
专家分:0
注 册:2004-11-9
收藏
得分:0 

太谢谢你了,斑竹,你是我见过的最好的斑竹,这么快回复,又如此详细,

thank you !!!!!

2004-11-26 11:04
live41
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:67
帖 子:12442
专家分:0
注 册:2004-7-22
收藏
得分:0 
  kai 你怎么经常3点多才上,想跟你聊都不行。
2004-11-26 18:35
快速回复:[求助]模版栈的问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.025088 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved