【问题标题】:How do I create a templated struct within the private section of a templated class如何在模板化类的私有部分中创建模板化结构
【发布时间】:2021-11-28 17:16:13
【问题描述】:

所以我一直在研究一个带有节点结构的链表类。当我在类之前定义模板化结构时,该类工作,但在类的私有部分中声明它时它不起作用。我特别需要使用模板 T 在类私有部分中定义 ListNode 结构。

#include <iostream>
using namespace std;

template <typename T>
class LinkedList
{
public:
    LinkedList()
    {
        head = NULL;
        tail = NULL;
        numNodes = 0;
    }
    ~LinkedList()
    {
    }
    int getLength()
    {
        return numNodes;
    }
    T getNodeValue(int value)
    {
        ListNode<T> *indexNode;
        indexNode = head;
        int i = 0;
        if (value < 0 || value > numNodes - 1)
        {
            throw;
        }
        while (indexNode->next != NULL)
        {
            if (i == value)
            {
                break;
            }
            indexNode = indexNode->next;
            i++;
        }
        return indexNode->contents;
    }
    void insertNode(ListNode<T> *newNode)
    {

        if (head == NULL)
        {
            head = newNode;
            tail = head;
            tail->next = NULL;
            numNodes++;
            return;
        }

        if (newNode->contents <= head->contents)
        {
            if (head == tail)
            {
                head = newNode;
                head->next = tail;
            }
            else
            {
                ListNode<T> *placeholder;
                placeholder = head;
                head = newNode;
                head->next = placeholder;
            }
            numNodes++;
            return;
        }
        if (newNode->contents > tail->contents)
        {
            if (head == tail)
            {
                tail = newNode;
                head->next = tail;
            }
            else
            {
                ListNode<T> *placeholder;
                placeholder = tail;
                tail = newNode;
                placeholder->next = tail;
            }
            numNodes++;
            return;
        }

        ListNode<T> *indexNode;
        indexNode = head;
        while (indexNode->next != NULL)
        {
            if (newNode->contents <= indexNode->next->contents)
            {
                newNode->next = indexNode->next;
                indexNode->next = newNode;
                numNodes++;
                return;
            }
            indexNode = indexNode->next;
        }
    }
private:
    template <typename T>
    struct ListNode
    {
        ListNode()
        {
        next = NULL;
        }
        ListNode(T value)
        {
            contents = value;
            next = NULL;
        }
        T contents;
        ListNode<T> *next;
    };
    ListNode<T> *head;
    ListNode<T> *tail;
    int numNodes;
};

#endif

【问题讨论】:

  • 这段代码有同样的问题:struct Foo { Bar bar; struct Bar { }; };

标签: c++ class templates struct


【解决方案1】:

您的结构 ListNode 不必是模板,因为它已经是在模板类中定义的嵌套类型。

只要这样定义:

    struct ListNode
    {
        ListNode()
        {
            next = NULL;
        }

        ListNode(T value)
        {
            contents = value;
            next = NULL;
        }

        T contents;

        ListNode *next;
    };

然后只需在类中使用LinkedList&lt;T&gt;::ListNode(或仅在类中使用ListNode)。

例如,成员headtail 变为:

ListNode *head;
ListNode *tail;

重要提示: 如果结构体用于公共函数(例如:void insertNode(ListNode *newNode)),则不能在私有部分定义结构体。否则,你应该如何创建一个ListNode 来从课堂外调用insertNode

【讨论】:

  • 谢谢,这解决了我的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多