还是按照编译器自己生成的声明,结果在实现取栈顶的时候出错了,不知道是哪里写的不对
const int MAX =10;
template <class T>
class stack{
public:
enum error_code{success,overflow,underflow};
stack();//初始化:设置栈为空 (采用c++的构造函数)
bool empty() const; //判断栈为空 若为空,则返回TRUE,否则返回FALSE
bool full() const; //若为满,则返回TRUE,否则返回FALSE
error_code get_top(T &x) const; //取栈顶元素 如果栈不空,则取出栈顶元素到参数x中,然后返回success,否则返回downflow
error_code push(const T x); //入栈 如果栈不满,则将元素入栈,并返回success,否则返回overflow
error_code pop(); //出栈 如果栈不空,删除当前栈顶的元素,并返回success,否则返回underflow
private:
int count;
T data[MAX];
};
template<class T>
error_code stack<T>::get_top(T &x) const {
if(empty()) return underflow;
else {
x = data[count - 1];
return success;
}
}
template<class T>
stack::error_code stack<T>::push(const T x){
if(full()) return overflow;
data[count] = x;
count ++;
return success;
}
template<class T>
stack::error_code stack<T>::pop(){
if(empty()) return underflow;
count--;
return success;
}
template<class T>
typename stack<T>::error_code stack<T>::push(const T x){
if(full()) return overflow;
data[count] = x;
count ++;
return success;
}