Home > Back-end >  How do I create a templated struct within the private section of a templated class
How do I create a templated struct within the private section of a templated class

Time:10-09

So I have been working on a linked list class with a node struct to go along with it. the class works when I define the templated struct before the class but it does not work when it is declared within the classes' private section. I specifically need the ListNode Struct to be defined within the class private section using the template T.

#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

CodePudding user response:

Your struct ListNode doesn't have to be a template because it is already a nested type defined inside a template class.

Just define it like this:

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

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

        T contents;

        ListNode *next;
    };

And then just use LinkedList<T>::ListNode (or just ListNode inside the class).

For instance, the members head and tail become:

ListNode *head;
ListNode *tail;

Important: You cannot define the struct in the private section if its used in public function (e.g.: void insertNode(ListNode *newNode)). Otherwise, how are you supposed to create a ListNode to call insertNode from outside the class?

  • Related