c++ - 错误 C2955 : use of class template requires template argument list

标签 c++ class templates

不确定为什么会出现此错误。类中的所有函数都已定义。我也尝试在 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/

相关文章:

java - 无法调用匿名类方法

c++ - 常量全局变量模板

c++ - 有没有办法将两个 QWidget 连接或锚定在一起?

c++ - 为什么复制构造函数在C++中直接使用私有(private)属性

Javascript:为什么在子类中声明属性会覆盖父类(super class)中的相同属性为null

c++ - 如何强制两个函数参数具有相同的模板类型?

C++循环展开性能

c++ - 使用枚举参数重载模板时出现 MSVC 编译器错误

c++ - CFile::osNoBuffer 标志在写入文件时导致异常

c++ - 重构实现