title: best practices: include .cpp file in main instead of .h file when using templates?
body: <p>I have a class Node with a template <br>
  this is header file</p>
  
  <pre><code>// node.h
  #ifndef NODE_H
  #define NODE_H
  #include &lt;iostream&gt;
  
  using namespace std;
  
  template &lt;typename T&gt;
  class Node
  {
  private:
      T content;
      Node&lt;T&gt; *next;
  public:
      Node();
      Node(T);
  
      template &lt;typename Y&gt;
      friend ostream &amp;operator &lt;&lt;(ostream &amp;, const Node&lt;Y&gt; &amp;);
  };
  #endif // NODE_H
  </code></pre>
  
  <p>and that's the implementation</p>
  
  <pre><code>// node.cpp
  #include "node.h"
  
  using namespace std;
  
  template&lt;typename T&gt;
  Node&lt;T&gt;::Node()
  {
      content = 0;
      next = NULL;
  }
  
  template&lt;typename T&gt;
  Node&lt;T&gt;::Node(T c)
  {
      content = c;
      next = NULL;
  }
  
  template&lt;typename Y&gt;
  ostream &amp;operator&lt;&lt;(ostream &amp;out, const Node&lt;Y&gt; &amp;node)
  {
      out &lt;&lt; node.content;
      return out;
  }
  </code></pre>
  
  <p>and here is the main file</p>
  
  <pre><code>// main.cpp
  #include &lt;iostream&gt;
  #include "node.cpp"
  
  using namespace std;
  
  int main()
  {
      Node&lt;int&gt; n1(5);
  
      cout &lt;&lt; n1;
  
      return 0;
  }
  </code></pre>
  
  <p>this way, it works perfectly, but i don't think it's good to include cpp file. <br>
  so i included node.h but i get this errors</p>
  
  <pre><code>pathtofile/main.cpp:8: error: undefined reference to `Node&lt;int&gt;::Node(int)'
  pathtofile/main.cpp:10: error: undefined reference to `std::ostream&amp; operator&lt;&lt; &lt;int&gt;(std::ostream&amp;, Node&lt;int&gt; const&amp;)'
  </code></pre>
  
  <p>I really want to follow best practices, and i always separate code declaration from implementation; but this my first time working with templates.</p>