【发布时间】:2018-03-14 18:30:43
【问题描述】:
给定这个示例类模板:
template<typename T>
class Stack {
T * data;
int size;
int nextIndex;
public:
Stack(int size = 100);
Stack(const Stack& stack);
~Stack();
Stack& operator=(const Stack& s);
void push(const T& t);
void pop();
T& top();
const T& top() const;
int getSize() const;
class Full {
};
class Empty {
};
};
template<typename T>
void Stack::push(const T& t) {
if (nextIndex >= size) {
throw Full();
}
data[nextIndex++] = t;
}
template<typename T>
void Stack::pop() {
if (nextIndex <= 0) {
throw Empty();
}
nextIndex--;
}
push 和 pop 方法的实现部分可以吗?
我不明白我是否需要写void Stack<T>::push(const T& t) 而不是void Stack::push(const T& t)(pop 方法也是如此)。
注意:Eclipse(根据 C++11)给了我下一个错误:
未找到成员声明
因为这些行:
void Stack::push(const T& t) {
void Stack::pop() {
【问题讨论】:
-
在旧的编译器中没问题,但现在你应该使用
void Stack<T>::。