为了账号安全,请及时绑定邮箱和手机立即绑定

求typedef Stack<int> IntStack; IntStack s; 这样实例化对吗?

求typedef Stack<int> IntStack; IntStack s; 这样实例化对吗?

白板的微信 2022-05-12 17:11:22
#include<iostream>using namespace std;template<class Type>class Stack{private:int top;Type *stacka;//用数组存储栈的元素int maxSize;public:Stack(int Size);//~Stack(){delete[] stack;}void push(const Type &item);Type pop(void);Type getTop();int empty(void) const{return top==-1;}int full(void) const{return maxSize-1;}void clear(void){top==-1;}};template<class Type>void Stack<Type> ::push(const Type &item){if(full()){cout<<"stack is full"<<endl;exit(1);}top++;stacka[top]=item;}template<class Type>Type Stack<Type> ::pop(){if(empty()){cout<<"stack is empty!"<<endl;exit(1);}Type data=stacka[top];//栈不空,取栈顶元素top--;return 0;//返回栈顶元素}template<class Type>Type Stack<Type> ::getTop(){if(empty()){cout<<"stack is empty!"<<endl;exit(1);}return stacka[top];//栈不空,取栈顶元素}int main(){typedef Stack<int> IntStack;IntStack s;for(int i=0;i<10;i++){s.push(i);}for(int i=0;i<10;i++){cout<<"翻转后:"<<s.pop()<<endl;}}报错是45 cpp no matching function for call to `Stack<int>::Stack()'cpp:4 candidates are: Stack<int>::Stack(const Stack<int>&)
查看完整描述

2 回答

?
泛舟湖上清波郎朗

TA贡献1818条经验 获得超3个赞

0、构造函数不匹配,需要传参,或者构造函数中设置参数默认值,比如 Stack(int Size=10);
1、缺少构造函数的定义
2、函数full中逻辑错误,可能改为maxSize-1==top;(跟maxsize的初始化值有关)
3、函数pop返回值错误,应该返回data
错误大体如上,其他你自己再看看吧。

查看完整回答
反对 回复 2022-05-16
?
莫回无

TA贡献1865条经验 获得超7个赞

你构造函数没定义,但是你的构造函数把空参构造函数取代了

查看完整回答
反对 回复 2022-05-16
  • 2 回答
  • 0 关注
  • 181 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号