title: "Undefined reference to" template class constructor
body: <p>I have no idea why this is happenning, since I think I have everything properly declared and defined.</p>
  <p>I have the following program, designed with templates. It's a simple implementation of a queue, with the member functions &quot;add&quot;, &quot;substract&quot; and &quot;print&quot;.</p>
  <p>I have defined the node for the queue in the fine &quot;nodo_colaypila.h&quot;:</p>
  <pre><code>#ifndef NODO_COLAYPILA_H
  #define NODO_COLAYPILA_H
  
  #include &lt;iostream&gt;
  
  template &lt;class T&gt; class cola;
  
  template &lt;class T&gt; class nodo_colaypila
  {
          T elem;
          nodo_colaypila&lt;T&gt;* sig;
          friend class cola&lt;T&gt;;
      public:
          nodo_colaypila(T, nodo_colaypila&lt;T&gt;*);
  
  };
  </code></pre>
  <p>Then the implementation in &quot;nodo_colaypila.cpp&quot;</p>
  <pre><code>#include &quot;nodo_colaypila.h&quot;
  #include &lt;iostream&gt;
  
  template &lt;class T&gt; nodo_colaypila&lt;T&gt;::nodo_colaypila(T a, nodo_colaypila&lt;T&gt;* siguiente = NULL)
  {
      elem = a;
      sig = siguiente;//ctor
  }
  </code></pre>
  <p>Afterwards, the definition and declaration of the queue template class and its functions:</p>
  <p>&quot;cola.h&quot;:</p>
  <pre><code>#ifndef COLA_H
  #define COLA_H
  
  #include &quot;nodo_colaypila.h&quot;
  
  template &lt;class T&gt; class cola
  {
          nodo_colaypila&lt;T&gt;* ult, pri;
      public:
          cola&lt;T&gt;();
          void anade(T&amp;);
          T saca();
          void print() const;
          virtual ~cola();
  
  };
  
  
  #endif // COLA_H
  </code></pre>
  <p>&quot;cola.cpp&quot;:</p>
  <pre><code>#include &quot;cola.h&quot;
  #include &quot;nodo_colaypila.h&quot;
  
  #include &lt;iostream&gt;
  
  using namespace std;
  
  template &lt;class T&gt; cola&lt;T&gt;::cola()
  {
      pri = NULL;
      ult = NULL;//ctor
  }
  
  template &lt;class T&gt; void cola&lt;T&gt;::anade(T&amp; valor)
  {
      nodo_colaypila &lt;T&gt; * nuevo;
  
      if (ult)
      {
          nuevo = new nodo_colaypila&lt;T&gt; (valor);
          ult-&gt;sig = nuevo;
          ult = nuevo;
      }
      if (!pri)
      {
          pri = nuevo;
      }
  }
  
  template &lt;class T&gt; T cola&lt;T&gt;::saca()
  {
      nodo_colaypila &lt;T&gt; * aux;
      T valor;
  
      aux = pri;
      if (!aux)
      {
          return 0;
      }
      pri = aux-&gt;sig;
      valor = aux-&gt;elem;
      delete aux;
      if(!pri)
      {
          ult = NULL;
      }
      return valor;
  }
  
  template &lt;class T&gt; cola&lt;T&gt;::~cola()
  {
      while(pri)
      {
          saca();
      }//dtor
  }
  
  template &lt;class T&gt; void cola&lt;T&gt;::print() const
  {
      nodo_colaypila &lt;T&gt; * aux;
      aux = pri;
      while(aux)
      {
          cout &lt;&lt; aux-&gt;elem &lt;&lt; endl;
          aux = aux-&gt;sig;
      }
  }
  </code></pre>
  <p>Then, I have a program to test these functions as follows:</p>
  <p>&quot;main.cpp&quot;</p>
  <pre><code>#include &lt;iostream&gt;
  #include &quot;cola.h&quot;
  #include &quot;nodo_colaypila.h&quot;
  
  using namespace std;
  
  int main()
  {
      float a, b, c;
      string d, e, f;
      cola&lt;float&gt; flo;
      cola&lt;string&gt; str;
  
      a = 3.14;
      b = 2.71;
      c = 6.02;
      flo.anade(a);
      flo.anade(b);
      flo.anade(c);
      flo.print();
      cout &lt;&lt; endl;
  
      d = &quot;John&quot;;
      e = &quot;Mark&quot;;
      f = &quot;Matthew&quot;;
      str.anade(d);
      str.anade(e);
      str.anade(f);
      cout &lt;&lt; endl;
  
      c = flo.saca();
      cout &lt;&lt; &quot;First In First Out Float: &quot; &lt;&lt; c &lt;&lt; endl;
      cout &lt;&lt; endl;
  
      f = str.saca();
      cout &lt;&lt; &quot;First In First Out String: &quot; &lt;&lt; f &lt;&lt; endl;
      cout &lt;&lt; endl;
  
      flo.print();
      cout &lt;&lt; endl;
      str.print();
  
      cout &lt;&lt; &quot;Hello world!&quot; &lt;&lt; endl;
      return 0;
  }
  </code></pre>
  <p>But when I build, the compiler throws errors in every instance of the template class:</p>
  <p><strong>undefined reference to `cola(float)::cola()'...</strong> (it's actually cola'&lt;'float'&gt;'::cola(), but this doesn't let me use it like that.)</p>
  <p>And so on. Altogether, 17 warnings, counting the ones for the member functions being called in the program.</p>
  <p>Why is this? Those functions and constructors WERE defined. I thought that the compiler could replace the &quot;T&quot; in the template with &quot;float&quot;, &quot;string&quot; or whatever; that was the advantage of using templates.</p>
  <p>I read somewhere here that I should put the declaration of each function in the header file for some reason. Is that right? And if so, why?</p>