【问题标题】:What step can I take to fix the error of “Unknown type name 'Node' ”?我可以采取什么步骤来修复“Unknown type name 'Node'”的错误?
【发布时间】:2021-08-26 19:52:20
【问题描述】:

我正在运行以下程序。在这个程序中,我必须实现一个双向链表。我仍然要完成这个程序。但是,请看下面我到目前为止的程序。我在函数中收到错误“未知类型名称'节点'”:

template<typename T>
Node* LinkedList<T>::Find(const T& val) const {
    Node *itr = nullptr;
    Node *ptr = head->next;
    while (ptr != tail) {
        if (ptr->data == val) {
            *itr = *ptr;
            break;
        }
        ptr = ptr->next;
    }
    return *itr;
}

我不明白为什么我会收到这个错误,我可以做些什么来解决它?

#include <iostream>
#include <vector>
using namespace std;

template<typename T>
class LinkedList{
public:
    struct Node {
        T data;
        Node* prev;
        Node* next;
    };
    void PrintForward();
    void PrintReverse();
    void PrintForwardRecursive(const Node*);
    void PrintReverseRecursive(const Node*);
    int NodeCount() const;
    void FindAll(vector<Node*>&, const T&);
    Node* Find(const T&) const;
    Node* GetNode(int& ) const;
    Node* Head() const;
    Node* Tail() const;
    void AddHead(const T&);
    void AddTail(const T&);
    void AddNodesHead(const T&, int);
    void AddNodesTail(const T&, int);
    void InsertAfter(Node*, const T&);
    void InsertBefore(Node*, const T&);
    void InsertAt(const T&, int);
    bool RemoveHead();
    bool RemoveTail();
    int Remove(const T&);
    bool RemoveAt(int);
    void clear();
    T& operator[](int);
    bool operator==(const LinkedList<T>&);
    LinkedList<T>& operator=(const LinkedList<T>&);
    LinkedList();
    LinkedList(const LinkedList<T>&);
    ~LinkedList();
private:
    int n;
    Node* head;
    Node* tail;
};

template<typename T>
    void LinkedList<T>::PrintForward() {
    PrintForwardRecursive(head->next);
}

template<typename T>
void LinkedList<T>::PrintReverse() {
    PrintReverseRecursive(tail->prev);
}

template<typename T>
void LinkedList<T>::PrintForwardRecursive(const Node* node) {
    cout << node->data << " ";
    if (node->next != tail)
        PrintForwardRecursive(node->next);
}

template<typename T>
void LinkedList<T>::PrintReverseRecursive(const Node* node) {
    cout << node->data << " ";
    if (node->next != prev)
        PrintForwardRecursive(node->prev);
}

template<typename T>
int LinkedList<T>::NodeCount() const {
    int num = 1;
    Node *ptr = head;
    while (ptr != tail) {
        ptr = ptr->next;
        num++;
    }
    return num;
}

template<typename T>
void LinkedList<T>::FindAll(vector<Node*>&matches, const T& val) {
    Node *ptr = head->next;
    while (ptr != tail) {
        if (ptr->data == val)
            matches.push_back(*ptr);
        ptr = ptr->next;
    }
}

template<typename T>
Node* LinkedList<T>::Find(const T& val) {
    Node *itr = nullptr;
    Node *ptr = head->next;
    while (ptr != tail) {
        if (ptr->data == val) {
            *itr = *ptr;
            break;
        }
        ptr = ptr->next;
    }
    return *itr;
}

template<typename T>
LinkedList<T>::LinkedList() {
    head = new Node;
    tail = new Node;
    head->next = tail;
    tail->prev = head;
    n = 0;
}

template<typename T>
LinkedList<T>::~LinkedList() {
    bool empty = false;
    while (!empty) {
        empty = RemoveHead();
    }
    delete head;
    delete tail;
}

int main() {
    std::cout << "Hello, World!" << std::endl;
    return 0;
}

【问题讨论】:

  • 为什么投反对票?

标签: c++ c++11 clion member-functions


【解决方案1】:

类型NodeLinkedList&lt;T&gt; 的成员。这意味着当你定义函数时,你需要使用

template<typename T>
typename LinkedList<T>::Node* LinkedList<T>::Find(const T& val) const

以便编译器知道您正在使用的Node 是来自LinkedListNode。需要 typename 让编译器知道 LinkedList&lt;T&gt;::Node 是成员类型,而不是成员对象。

【讨论】:

  • 我试过了,但现在我收到另一个错误消息“在依赖类型名称 'LinkedList::Node' 之前缺少 'typename'”。
  • @ArunabhBhattacharya 我刚刚确定了答案。你需要typename LinkedList&lt;T&gt;::Node*
猜你喜欢
  • 2021-06-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-10
  • 1970-01-01
相关资源
最近更新 更多