纠结郁闷有木有啊 标题要长长长长长长长长长长长长长长长才有人看【C++】

风物长宜放眼量~ 2012-05-29 02:14:52
以个栈的模版
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;

template<class Type>

class Stack{
int stacksize;
Type *items;
int top;
public:
explicit Stack(int ss);
Stack(const Stack &st);
~Stack(){delete [] items;}
void push(const Type &item);
Type pop();
Stack &operator=(const Stack &st);
};

template <class Type>
inline Stack<Type>::Stack(int ss=0):
stacksize(ss),top(0){
items=new Type[stacksize];
}


template <class Type>
Stack<Type>::Stack(const Stack &st){
stacksize=st.stacksize;
top=st.top;
items=new Type [stacksize];
for(int i=0;i<top;i++){
items[i]=st.items[i];
}
}

template <class Type>
inline void Stack<Type>::push(const Type& item){

if(top<stacksize)

items[top++]=item;

else
cout<<"the stack is full"<<endl;
}

template <class Type>
inline Type Stack<Type>::pop(){
if(top>0)

return items[top--];
else
cout<<"the stack is empty!"<<endl;
}


template <class Type>
inline Stack<Type> &Stack<Type>::operator =(const Stack<Type> &st){
if(this==&st)
return *this;
delete [] items;
stacksize=st.stacksize;
top=st.top;
items=new Type [stacksize];
for(int i=0;i<top;i++)
items[i]=st.items[i];
return *this;
}


int main(){
int m;char *str;
cout<<"enter stack size:"<<endl;
cin>>m;
if(m<=0) cout<<"error of size"<<endl;
else
Stack<char> st (20);它说这个st没定义,郁闷死了- -
str=new char[m];
cin>>str;
for(int i=0;i<m;i++){
st.push(str[i]);
}
for(int j=0;j<m;j++){
cout<<st.pop()<<endl;
}
cout<<endl;
delete [] str;
return 0;

}
...全文
64 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
liangbch 2012-05-29
  • 打赏
  • 举报
回复
楼主啦,没见过你这么用的。

错误1:
“Stack<char> st (20)"应该改为
Stack<char> st(20);

错误2:
“Stack<char> st (20);”
仅仅这个定义放在else分支,而其后的代码不属于else分支,故无法引用到这个st定义。
“for(int i=0;i<m;i++){
st.push(str[i]);”

下面是我给你修订后的代码。另外,楼主的代码太乱了,你就不能格式化一下吗?

#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;

template<class Type>

class Stack{
int stacksize;
Type *items;
int top;
public:
explicit Stack(int ss);
Stack(const Stack &st);
~Stack(){delete [] items;}
void push(const Type &item);
Type pop();
Stack &operator=(const Stack &st);
};

template <class Type>
inline Stack<Type>::Stack(int ss=0):
stacksize(ss),top(0){
items=new Type[stacksize];
}


template <class Type>
Stack<Type>::Stack(const Stack &st){
stacksize=st.stacksize;
top=st.top;
items=new Type [stacksize];
for(int i=0;i<top;i++){
items[i]=st.items[i];
}
}

template <class Type>
inline void Stack<Type>::push(const Type& item){

if(top<stacksize)

items[top++]=item;

else
cout<<"the stack is full"<<endl;
}

template <class Type>
inline Type Stack<Type>::pop(){
if(top>0)

return items[top--];
else
cout<<"the stack is empty!"<<endl;
}


template <class Type>
inline Stack<Type> &Stack<Type>::operator =(const Stack<Type> &st){
if(this==&st)
return *this;
delete [] items;
stacksize=st.stacksize;
top=st.top;
items=new Type [stacksize];
for(int i=0;i<top;i++)
items[i]=st.items[i];
return *this;
}


int main()
{
int m;char *str;
cout<<"enter stack size:"<<endl;
cin>>m;

if(m<=0)
cout<<"error of size"<<endl;
else
{
Stack<char> st(20);
str=new char[m];
cin>>str;

for(int i=0;i<m;i++){
st.push(str[i]);
}
for(int j=0;j<m;j++){
cout<<st.pop()<<endl;
}
cout<<endl;
delete [] str;
}
return 0;

}


  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;

template<class Type>

class Stack{
int stacksize;
Type *items;
int top;
public:
explicit Stack(int ss);
Stack……
[/Quote]什么意思??????????
jlg1140410822 2012-05-29
  • 打赏
  • 举报
回复
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;

template<class Type>

class Stack{
int stacksize;
Type *items;
int top;
public:
explicit Stack(int ss);
Stack(const Stack &st);
~Stack(){delete [] items;}
void push(const Type &item);
Type pop();
Stack &operator=(const Stack &st);
};

64,685

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧