简体   繁体   English

如何解决“使用类模板需要模板参数列表”

[英]How to fix “use of class template requires template argument list”

i made some code about stack using template. 我用模板做了一些关于堆栈的代码。 but right now, stuck with the little error. 但是现在,仍然停留在小错误上。 i tried many times as i can but don't know what to do. 我尝试了很多次,但是不知道该怎么办。 also, i see the a lot of same error question but my one is seems to be different. 另外,我看到很多相同的错误问题,但我的问题似乎有所不同。

I'm middle of the writing. 我在写作中。 so, there is some uncompleted code. 因此,有一些未完成的代码。 if you see something wrong but not cause the error, ignore it please. 如果您看到错误的内容但没有导致错误,请忽略它。

#ifndef STACKINTERFACE_H
#define STACKINTERFACE_H

using namespace std;

template <typename T>
class StackInterface {
public:
    StackInterface() {};
    virtual ~StackInterface() {};
    virtual bool isEmpty() const = 0;
    virtual bool isFull() const = 0;
    virtual void push(T data) = 0;
    virtual void pop() = 0;
    virtual T top() const = 0;
};


#include "StackInterface.h"

#include <iostream>
template <typename T>
class Stack : public StackInterface {
private:
    T * items;
    const static int max = 10;
    const static int GROWBY = 5;
    size_t arrayCapacity;
    size_t numItems;
    int top_position;
public:
    Stack();
    virtual void push(T data);
    virtual T top() const;
    virtual void pop();
    virtual bool isEmpty() const;
    virtual bool isFull() const;
    size_t getArrayCapacity();
    size_t getNumItems();
    Stack<T> operator=(const Stack<T>&Other);
    ~Stack();
    Stack(const Stack<T>& other);
};

template <typename T>
Stack<T>::Stack() 
{
    items = new T[max];
    numItems = 0;
    top_position = -1;
}

template <typename T>
Stack<T>::~Stack() {
    cout << "deleting..." << endl;
    delete[] items;
}

template <typename T>
void Stack<T>::push(T data) {
    cout << "inserting" << data << endl;
    if (numItems < arrayCapacity) {
        T[++top_position] = data;
    }
    numItems++;
}
template <typename T>
void Stack<T>::pop() {
    if (isEmpty) {
        cout << "The stack is empty. Returned value not reliable.\n";
    }
    else {
        cout << "removing" << top() << endl;
        retrun T[top_position--];
    }
}

template <typename T>
bool Stack<T>::isEmpty() const
{
    if (numItems == 0) {
        return true;
    }
    else {
        return false;
    }
}

template <typename T>
bool Stack<T>::isFull() const
{
    if (numItems == max) {
        return true;
    }
    else {
        return false;
    }
}

template <typename T>
T Stack<T>::top() const
{
    if (!isEmpty()) {
        return T[top_position];
    }
    else {
        throw exception;
    }
}

template <typename T>
Stack <T> Stack<T>:: operator=(const Stack<T>&Other) {
    if (&Other == this) {
        return *this;
    }   
}

template <typename T>
Stack<T>::Stack(const Stack<T>& other) {
    items = new items(other.items);
    numItems = other.numItems;
    top_position = other.top_position;
    arrayCapacity = other.arrayCapacity;
}

template <typename T>
size_t Stack<T>::getArrayCapacity() {
    return arrayCapacity;
}

template <typename T>
size_t Stack<T>::getNumItems() {
    return numItems;
}

int main() {

    Stack <int> S1;


    system("pause");
    return 0;
}

You need to give template arguments to your superclass. 您需要为超类提供模板参数。 Instead of this: 代替这个:

template <typename T>
class Stack : public StackInterface {

Do this: 做这个:

template <typename T>
class Stack : public StackInterface<T> {

(There's lots of other problems with your code too, but that's how you fix the specific one you asked about.) (您的代码还有很多其他问题,但这就是您解决所要求的特定问题的方法。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM