title: Simple class with template
body: <p>We are trying to write a really simple class, for complex numbers jsut as an example, and we do not get very far...</p>
  
  <p>Below is our 3 files.</p>
  
  <p><strong>complex2.h</strong></p>
  
  <pre><code>#include&lt;iostream&gt;
  #include&lt;new&gt;
  
  template&lt;class T&gt;
  class complex2
  {
  private:
     T re, im; // real and imaginary part
  public:
      complex2();
      complex2(T re_a =0.0, T im_a =0.0); //= 0.0 = 0.0
  
      ~complex2() {}
      T Re () const;
      T Im () const;
  };
  
  #endif // COMPLEX2_H
  </code></pre>
  
  <p><strong>complex2.cpp</strong></p>
  
  <pre><code>#include "complex2.h"
  
  template&lt;class T&gt;
  complex2&lt;T&gt;:: complex2 () {re = im = 0.0; }
  
  template&lt;class T&gt;
  complex2&lt;T&gt;:: complex2(T re_a, T im_a){re = re_a; im = im_a;}
  
  template&lt;class T&gt; T complex2&lt;T&gt;:: Re() const { return re;}
  
  template&lt;class T&gt; T complex2&lt;T&gt;:: Im() const {return im;}
  </code></pre>
  
  <p><strong>main.cpp</strong></p>
  
  <pre><code>#include &lt;iostream&gt;
  #include&lt;cmath&gt;
  #include"complex2.h"
  
  using namespace std;
  
  int main()
  {
      complex2&lt;int&gt; b(1, 2);//
      cout &lt;&lt; "Re b: "&lt;&lt; b.Re() &lt;&lt; "Im b: "&lt;&lt; b.Im() &lt;&lt; endl;
      return 0;
  }
  </code></pre>
  
  <p>Running the above from Qt, gives the error messages</p>
  
  <pre><code>/home...main.cpp:10: error: undefined reference to `complex2&lt;int&gt;::complex2(int, int)'
  
  /home/.../main.cpp:11: error: undefined reference to `complex2&lt;int&gt;::Im() const'
  
  /home/...main.cpp:11: error: undefined reference to `complex2&lt;int&gt;::Re() const'
  
  :-1: error: collect2: error: ld returned 1 exit status
  </code></pre>
  
  <p>Does anybody see how we can make this work?</p>