-1
//linkstack.h
##ifndef LINKSTACK_H_INCLUDED
##define LINKSTACK_H_INCLUDED
##include<iostream>
using namespace std;
template <typename T>
class LinkStack{
private:
    T* mc_lin;    //栈指针
    int mc_size; //最大容量
    int mc_top; //栈顶
public:
    LinkStack(const LinkStack<T>& a);//深拷贝 
    LinkStack(T size); //构造函数
    ~LinkStack(); //析构函数
    bool stackEmpty();
    bool stackFull();
    LinkStack<T>* clearStack();//清空栈中的元素
    LinkStack<T>*  push(T elem);//压栈
    LinkStack<T>* pop(T &elem);//弹栈
    bool getTop(T &elem);//获取栈顶元素
    int stackLength();//栈的长度
    void traverse();//遍历栈
    };
template <typename T>
LinkStack(const LinkStack<T>& a)
{
    mc_lin=a.mc_lin;
    mc_size=a.mc_size;
    mc_top=a.mc_top;
}
template <typename T>
LinkStack<T>::LinkStack(T size){
    this->mc_size=size;
    this->mc_lin=new T[mc_size];
    this->mc_top=0;

}
template <typename T>
LinkStack<T>::~LinkStack(){
    delete this->mc_lin;
    this->mc_lin=NULL; 
}
template <typename T>
bool LinkStack<T>::stackEmpty(){
    return this->mc_top==0?true:false;
}
template <typename T>
bool LinkStack<T>::stackFull(){
    return this->mc_top>=this->mc_size?true:false;
}
template <typename T>
LinkStack<T>* LinkStack<T>::clearStack(){
    this->mc_top=0;
    return this;
}
template <typename T>
LinkStack<T>* LinkStack<T>::push(T elem){
    if(this->stackFull()) return this;
    else{
    this->mc_lin[this->mc_top++]=elem;
    return this;
    }

}
template <typename T>
LinkStack<T>* LinkStack<T>::pop(T &elem){
    if(this->stackEmpty()) return this;
    else{
        elem=this->mc_lin[--this->mc_top];
        return this;
    }
}
template <typename T>
bool LinkStack<T>::getTop(T &elem){
    if(this->stackEmpty()) return false;
    else{
        elem=this->mc_lin[this->mc_top-1];
        return true;
    }
}
template <typename T>
int LinkStack<T>::stackLength(){
    return this->mc_top;
}
template <typename T>
void LinkStack<T>::traverse(){
    for(int i=0;i<this->mc_top;i++)
        cout<<this->mc_lin[i]<<"\t";
}

##endif // LINKSTACK_H_INCLUDED

不知哪里出错,还望各位大佬看到指出

1 个回答

0

已采纳

definition处改成这样:

LinkStack<T>::LinkStack(const LinkStack<T>& a)

另外你把代码贴贴好行吗...