如何在一个类中成员函数里面调用这个类的模板函数

 我来答
零二捌1378
2019-05-29 · TA获得超过9238个赞
知道大有可为答主
回答量:9853
采纳率:69%
帮助的人:497万
展开全部
#ifndef STACK_H #define STACK_H #include #include /** * 栈 */ template class Stack { public: Stack(); public: /** * 元素入栈 * @param item 需要入栈的元素 */ void push(const ItemType& item); /** * 栈顶元素出栈 * @return 栈顶的元素 */ ItemType pop(); /** * 获取栈顶元素 * 栈顶元素 */ ItemType peek() const; /** * 清空栈 */ void clearStack(); private: /** * 栈是否为空 * @return true 栈为空;false 栈不为空 */ bool isEmpty() const; /** * 栈是否为满 * @return true 栈为满;false 栈不为满 */ bool isFull() const; private: /** * 栈的大小 */ static const int maxStackSize = 50; private: /** * 栈 */ ItemType array[maxStackSize]; /** * 栈顶标识 */ int top; }; template Stack::Stack() : top(-1) {} template void Stack::push(const ItemType& item) { if (isFull()) { std::cerr << "Stack overflow!" << std::endl; exit(EXIT_FAILURE); } top++; array[top] = item; } template ItemType Stack::pop() { if (isEmpty()) { std::cerr << "Attempt to pop an empty stack!" << std::endl; exit(EXIT_FAILURE); } ItemType temp = array[top]; top--; return temp; } template ItemType Stack::peek() const { if (isEmpty()) { std::cerr << "Attempt to peek an empty stack!" << std::endl; exit(EXIT_FAILURE); } return array[top]; } template bool Stack::isEmpty() const { return (top == -1 ? true : false); } template bool Stack::isFull() const { return (top == (maxStackSize - 1) ? true : false); } template void Stack::clearStack() { //将所有元素弹出栈 while (!isEmpty()) { pop(); } } #endif //STACK_H
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式