未找到继承模板的函数标识符[重复]

问题描述 投票:0回答:0

我正在使用 C++ 中的继承模板学习 LinkedLists 和 Stacks,并希望获得这部分的指导:

template<typename T>
class Stack : public SingleLinkedList<T> {
public:
    void push(T data) {
        addToHead(data);
    }

    T pop() {
        return removeFromHead();
    }
};

类型

SingleLinkedList
包含上述功能,并且大概定义正确。

但是我得到这个复杂的错误说找不到

removeFromHead

error C3861: 'removeFromHead': identifier not found
message : 'removeFromHead': function declaration must be available as none of the arguments depend on a template parameter
 message : see reference to class template instantiation 'Stack<T>' being compiled

SLL 和 Node 实现是:

template<typename T>
class SingleLinkedList {
public:
    Node<T>* head;

    SingleLinkedList() {
        head = nullptr;
    }

    ~SingleLinkedList() {
        auto n = head;
        while (n != nullptr) {
            auto tmp = n->next;
            delete n;
            n = tmp;
        }
    }

    void addToHead(T data) {
        if (head == nullptr) {
            head = new Node<T>(data, nullptr);
        } else {
            auto tmp = new Node<T>(data, head);
            head = tmp;
        }
    }

    T removeFromHead() {
        auto e = head->data;
        if (head != nullptr) {
            auto tmp = head;
            head = head->next;
            delete tmp;
        }
        return e;
    }
};
template<typename T>
class Node {
public:
    T data;
    Node* next;

    Node(T data, Node* next) {
        this->data = data;
        this->next = next;
    }

    Node(T data) {
        Node(data, nullptr);
    }
};
c++ templates inheritance stack
© www.soinside.com 2019 - 2024. All rights reserved.