[英]error C2955: use of class template requires template argument list
Not sure why I'm getting this error. 不知道为什么我得到这个错误。 All the functions in the class are defined.
定义了该类中的所有功能。 I tried putting a value inside T as well and nothing happened.
我也尝试在T中放入一个值,但没有任何反应。 I keep receiving this error "error C2955: use of class template requires template argument list"
我不断收到此错误“错误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();
}
You need to decide what type of stack you are going to have here. 您需要确定这里要使用的堆栈类型。
Stack<int> s;
This will make a stack where type T is int. 这将创建一个堆栈,其中T类型为int。 You can use other types here too.
您也可以在这里使用其他类型。 Lets say you want a stack of floats.
假设您要一堆花车。
Stack<float> s;
etc. 等等
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.