title: C++: Simple Node/Linked List
body: <p>In the following Linked List I am trying to implement a print function. The function is templated, and is not a part of the Node class.</p>
  
  <p>Basically I want this print function to be dynamic, so that I don't have to print out all of the <code>Node-&gt;data</code> manually. I am kind of working along the lines of this example: <a href="http://www.cstutoringcenter.com/tutorials/cpp/cpp17.php" rel="nofollow">http://www.cstutoringcenter.com/tutorials/cpp/cpp17.php</a> </p>
  
  <p>However, when I try to implement the print function I get compiler errors such as:
  <code>node was not declared in this scope</code>, <code>p' was not declared in this scope</code>, and <code>variable or field 'print' declared void</code>.</p>
  
  <p>Here is my program:</p>
  
  <pre><code>#include&lt;iostream&gt;
  using namespace std;
  
  template&lt;typename T&gt;
  class Node
  {
   public:
          Node(T = 0);
          ~Node() { delete [] nextPtr; };
  
          T getData() const;
          Node&lt;T&gt;*&amp; getNextPtr() { return nextPtr; };
  
   private:
          T data;
          Node&lt;T&gt; *nextPtr;    
  };
  
  //CONSTRUCTOR   
  template&lt;typename T&gt;
  Node&lt;T&gt;::Node(T newVal)
       : data(newVal), nextPtr(NULL)
  {
      //EMPTY       
  };
  
  //GETDATA() -- RETURN DATA VALUE
  template&lt;typename T&gt;
  T Node&lt;T&gt;::getData() const
  {
        return data;
  
  };
  
  //PRINT FUNCTION
  template&lt;typename T&gt;
  void print(node&lt;T&gt;* p)
  {
       while(p)
       {
               cout &lt;&lt; p-&gt;data();
               p = p-&gt;link();        
       }
  };
  
  int main()
  {
      Node&lt;int&gt; intNode1(5),  intNode2(6), intNode3(7);
  
      intNode1.getNextPtr() = &amp;intNode2;
      intNode2.getNextPtr() = &amp;intNode3;
  
      print(&amp;intNode1);
  
      system("pause");   
  }
  </code></pre>
  
  <p>What am I doing wrong?</p>