不确定为什么会出现此错误。类中的所有函数都已定义。我也尝试在 T 中放入一个值,但什么也没发生。我不断收到此错误“错误 C2955:类模板的使用需要模板参数列表”
template< class T >
class Stack {
public:
Stack(int = 10); // default constructor (stack size 10)
// destructor
~Stack() {
delete[] stackPtr;
}
bool push(const T&);
bool pop(T&);
// determine whether Stack is empty
bool isEmpty() const {
return top == -1;
}
// determine whether Stack is full
bool isFull() const {
return top == size - 1;
}
private:
int size; // # of elements in the stack
int top; // location of the top element
T *stackPtr; // pointer to the stack
};
// constructor
template< class T >
Stack< T >::Stack(int s) {
size = s > 0 ? s : 10;
top = -1; // Stack initially empty
stackPtr = new T[size]; // allocate memory for elements
}
template< class T >
bool Stack< T >::push(const T &pushValue) {
if (!isFull()) {
stackPtr[++top] = pushValue;
return true;
}
return false;
}
template< class T >
bool Stack< T >::pop(T &popValue) {
if (!isEmpty()) {
popValue = stackPtr[top--]; // remove item from Stack
return true;
}
return false;
}
int main() {
Stack s();
}
最佳答案
您需要决定您将在此处拥有什么类型的堆栈。
Stack<int> s;
这将创建一个类型 T 为 int 的堆栈。您也可以在这里使用其他类型。假设您想要一堆花车。
Stack<float> s;
等等
关于c++ - 错误 C2955 : use of class template requires template argument list,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38081607/