Templated nodes



For some reason, I don't think it's recognizing my constructor. I've tried replacing it with the line m_head = new Node<T>();, which actually compiles. However, I don't think that's the right syntax for calling the constructor. Any ideas on what's wrong?
Last edited on
> However, I don't think that's the right syntax for calling the constructor.
yes, it is.
There is no `Node' class, there is `Node<T>'

If `Node' were an inner class then
1
2
3
4
5
6
7
8
9
10
template <class T>
class List{
   class Node{
      //...
   };
   //...
   LinkedList(){
      m_head = new Node(); //because now its full name is List<T>::Node and we are inside List<T>
   }
};



> using templates so it accepts multiple data types
you may have List<int>, List<double>, List<std::string>, but all the nodes in the list would hold the same data type.



1
2
  //Dummy node.
  m_head = new Node();
If you are going to use a dummy (which is a good decision) I would recommend it to make it an object instead of a pointer.
Also, make the list circular, then you would not have to worry about dereferencing an invalid pointer.
I see! I didn't know that was correct syntax.Thanks for clearing that up!
Last edited on
Topic archived. No new replies allowed.