0

I am trying to make a linked list in C++ using Generics. But when I declare a Node object inside my class I get this error library.h:7:3: error: 'Node' does not name a type Node<T> n;

template <typename T>
class LinkedList{
public:
  int length;

  Node<T> n;

  LinkedList(){

  }

};

template <typename T>
class Node{
public:
  int value;
  Node *next;
  Node(int value){
    this->value = value;
  }
};
Valentin
  • 1,159
  • 1
  • 11
  • 22
  • 1
    You need to flip the definitions around since you can't use something you haven't declared yet. – NathanOliver Nov 07 '19 at 21:28
  • And how can I do this? – Valentin Nov 07 '19 at 21:29
  • 1
    Move the code for `LinkedList` to be after `Node` – NathanOliver Nov 07 '19 at 21:30
  • Or even better @Nathan, stick `Node` in `LinkedList` since it's an implementation detail that no one outside of the class should know or care about. – scohe001 Nov 07 '19 at 21:32
  • Now I get another error, but if I declare the node as ```Node* n;``` I don't get any errors. Should I let the '*' there? – Valentin Nov 07 '19 at 21:36
  • 1
    `Node n;` tries to create a new `Node` using the default constructor. But since you've declared a constructor that takes an `int`, no default constructor is created for you--so none exists! Creating a pointer isn't actually creating an object, so no constructor is called, which is why that works. Either specify a value when you create the `Node`, or create a pointer. – scohe001 Nov 10 '19 at 01:05

0 Answers0