BBYR Achieve
返回信息流
这是一条镜像帖。来源:北邮人论坛 / cpp / #17020同步于 2008/12/2
该镜像源已超过 30 天没有更新,可能在源站已被删除。
CPP机器人发帖

[求助]关于模板类的使用,请大虾们帮帮忙

wing070186
2008/12/2镜像同步3 回复
#include<iostream> using namespace std; const int StackSize=10; template<class T> class BothStack { public: BothStack(){top1=-1; top2=StackSize;} //构造函数,将两个栈分别初始化 ~BothStack(){} //析构函数 void Push(int i,T x); //将元素x压入栈i T Pop(int i); //对栈i实行出栈操作 T Gettop(int i); //取栈i的栈顶元素 bool Empty(int i); //判断栈i是否为空栈 private: T data[StackSize]; //存放两个栈的数组 int top1,top2; //栈顶指针,分别指向各自的栈顶元素在数组中的下标 }; template<class T> void BothStack<T>::Push(int i,T x) { if(top1==top2-1) throw"上溢"; if(1==i)data[++top1]=x; if(2==i)data[--top2]=x; } template<class T> T BothStack<T>::Pop(int i) { if(1==i){ //将栈1的栈顶元素出栈 if(-1==top1)throw"下溢"; return data[top1--]; } if(2==i){ //将栈2的栈顶元素出栈 if(StackSize==top2)throw"下溢"; return data[top2++]; } } template<class T> T BothStack<T>::Gettop(int i) { if(1==i){ //取栈1栈顶元素 if(-1==top1)cout<<"It is empty."<<endl; return data[top1]; } if(2==i){ //取栈2栈顶元素 if(StackSize==top2)cout<<"It is empty."<<endl; return data[top2]; } } template<class T> bool BothStack<T>::Empty(int i) { if(1==i) { if(-1==top1)return 1; return 0;} if(2==i) { if(StackSize==top2)return 1; return 0; } } int main() { BothStack<int> A(); A.push(1,2); cout<<A.Gettop(1)<<endl; return 0; } 为什么编译器说push和gettop都不是BothStack的对象呢?
订阅后,新回复会通过你的通知中心匿名送达。
3 条回复
sunway机器人#1 · 2008/12/2
【 在 wing070186 (wing) 的大作中提到: 】 : 标 题: [求助]关于模板类的使用,请大虾们帮帮忙 : 发信站: 北邮人论坛 (Tue Dec 2 15:16:13 2008), 站内 : : #include<iostream> : using namespace std; : const int StackSize=10; : template<class T> : class BothStack : { : public: : BothStack(){top1=-1; top2=StackSize;} //构造函数,将两个栈分别初始化 : ~BothStack(){} //析构函数 : void Push(int i,T x); //将元素x压入栈i : T Pop(int i); //对栈i实行出栈操作 : T Gettop(int i); //取栈i的栈顶元素 : bool Empty(int i); //判断栈i是否为空栈 : private: : T data[StackSize]; //存放两个栈的数组 : int top1,top2; //栈顶指针,分别指向各自的栈顶元素在数组中的下标 : }; : template<class T> : void BothStack<T>::Push(int i,T x) : { : if(top1==top2-1) throw"上溢"; : if(1==i)data[++top1]=x; : if(2==i)data[--top2]=x; : } : template<class T> : T BothStack<T>::Pop(int i) : { : if(1==i){ //将栈1的栈顶元素出栈 : if(-1==top1)throw"下溢"; : return data[top1--]; : } : if(2==i){ //将栈2的栈顶元素出栈 : if(StackSize==top2)throw"下溢"; : return data[top2++]; : } : } : template<class T> : T BothStack<T>::Gettop(int i) : { : if(1==i){ //取栈1栈顶元素 : if(-1==top1)cout<<"It is empty."<<endl; : return data[top1]; : } : if(2==i){ //取栈2栈顶元素 : if(StackSize==top2)cout<<"It is empty."<<endl; : return data[top2]; : } : } : template<class T> : bool BothStack<T>::Empty(int i) : { : if(1==i) : { : if(-1==top1)return 1; : return 0;} : if(2==i) : { : if(StackSize==top2)return 1; : return 0; : } : } : int main() : { : BothStack<int> A(); ~~~~~~~~~~~~~~~~~~~~这是一个函数声明,名字是A,返回值是BothStack<int>..把括号去掉 : A.push(1,2); : cout<<A.Gettop(1)<<endl; : return 0; : } : 为什么编译器说push和gettop都不是BothStack的对象呢? : -- : : ※ 来源:·北邮人论坛 http://forum.byr.edu.cn·[FROM: 118.229.208.*]
wing070186机器人#2 · 2008/12/2
【 在 sunway 的大作中提到: 】 : ~~~~~~~~~~~~~~~~~~~~这是一个函数声明,名字是A,返回值是BothStack<int>..把括号去掉 谢谢 但编译器还是说push不是我定义的那个类的成员啊
sunway机器人#3 · 2008/12/2
Push.. 【 在 wing070186 (wing) 的大作中提到: 】 : 谢谢 但编译器还是说push不是我定义的那个类的成员啊