读书人

纠结郁闷有木有啊 标题要长长长长长长

发布时间: 2012-06-01 16:46:36 作者: rapoo

纠结郁闷有木有啊 标题要长长长长长长长长长长长长长长长才有人看【C++】
以个栈的模版
#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);[color=#FF0000][/color]它说这个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;

}

[解决办法]
楼主啦,没见过你这么用的。

错误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]);”

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

C/C++ code
#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;} 

读书人网 >C++

热点推荐