1

I'm supposed to create a linked list (asked to put everything for now in header file) and I am trying to create a struct for nodes but it says it needs a class name. What did I do wrong? I am a bit confused on using a struct to create nodes for the list.

#include "LinkedListInterface.h"

#ifndef LAB03LINKEDLIST_LINKEDLIST_H
#define LAB03LINKEDLIST_LINKEDLIST_H


template <typename T>
class LinkedList: public LinkedListInterface<T>
{
public:    // this is where the functions go
    LinkedList();
    void AddNode(T addData)
    {
        nodePtr n = new node;
        n->next = NULL;
        n->data = addData;

        if (head != NULL)
        {
            current = head;
            while (current->next != NULL)
            {
                current = current->next;
            }
            current->next = n;
        }
        else
        {
            head = n;
        }
    }
    void DeleteNode(T delData);
    void PrintList();
private:
    struct node:
    {
    T data;
    node* next;
    };
    typedef struct node* nodePtr;
    nodePtr head;
    nodePtr current;
    nodePtr temp;
};



#endif //LAB03LINKEDLIST_LINKEDLIST_H
4
  • 3
    Remove the colon (:) after struct node Commented Jul 8, 2020 at 8:29
  • Because this is template code you should put everything in the header file. If you ever try to move it out of the header file that would be a mistake. Commented Jul 8, 2020 at 8:31
  • 3
    You already have the response from @Yksisarvinen . But also your #include should be inside the #ifndef LAB03LINKEDLIST_LINKEDLIST_H block, not outside. This can reduce considerable the time of compile. Commented Jul 8, 2020 at 8:42
  • Is AddNode declared as virtual void AddNode(T) = 0; in the CRTP class LinkedListInterface? Commented Jul 8, 2020 at 8:45

1 Answer 1

1
struct node:
{
    T data;
    node* next;
};

Should be

struct node
{
    T data;
    node* next;
};

There is no : after a class name unless you intend to use inheritance - @john

Sign up to request clarification or add additional context in comments.

2 Comments

Unless you are intending to use inheritance (which is probably what confused the OP).
Thank you friends!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.