【问题标题】:error C2955: use of class template requires template argument list错误 C2955:使用类模板需要模板参数列表
【发布时间】:2016-06-28 16:30:14
【问题描述】:

不知道为什么我会收到此错误。类中的所有函数都已定义。我也尝试在 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();

    }

【问题讨论】:

  • 您希望s 成为什么的堆栈?
  • 其实不行,你想让s返回一堆什么?

标签: c++ class templates


【解决方案1】:

您需要在这里决定要使用哪种类型的堆栈。

Stack<int> s;

这将创建一个类型 T 为 int 的堆栈。您也可以在此处使用其他类型。假设你想要一堆花车。

Stack<float> s;

等等

【讨论】:

  • 真不敢相信我错过了...非常感谢
  • 最麻烦的解析。你在这里没有声明一个变量,而是一个函数。
  • 使用Stack&lt;float&gt; s;使用默认ctor创建s
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-05
  • 1970-01-01
相关资源
最近更新 更多