title: C++ undefined reference to template class method
body: <p>I always get </p>
  
  <blockquote>
    <p>undefined reference to `Graph::InsertVertex(std::string)'</p>
  </blockquote>
  
  <p>if I compile my project! Any hints why he cant resolve this reference?
  (all Files are in the netbeans project folder)</p>
  
  <p>// <strong>main.cpp</strong></p>
  
  <pre><code>#include &lt;cstdlib&gt;
  #include &lt;string&gt;
  #include "Graph.h"
  
  using namespace std;
  
  int main(int argc, char** argv)
  {
      Graph&lt;string&gt; *graph = new Graph&lt;string&gt;(); // &lt;--- ERROR
  
      graph-&gt;InsertVertex("A");
  
      return 0;
  }
  </code></pre>
  
  <p>// <strong>Node.h</strong></p>
  
  <pre><code>#include &lt;iostream&gt;
  #include "Graph.h"
  
  template&lt;class T&gt; 
  class Node
  {   
  
  friend class Graph;    
  
  public:
      Node(T val)
      {
          this-&gt;data = val;
          this-&gt;vertList = NULL;
          this-&gt;next = NULL;
      }
  
      Node(const Node&amp; orig);
      virtual ~Node();
  
  private:
      T data;
      Node&lt;T&gt; *vertList;
      Node&lt;T&gt; *next;
      int status;
  
  };
  </code></pre>
  
  <p>// <strong>Graph.h</strong></p>
  
  <pre><code>#include &lt;iostream&gt;
  #include "Node.h"
  
  template &lt;class T&gt; 
  class Graph 
  {    
  public:
      Graph()
      {
          head = NULL;        
      }
  
      void InsertVertex(T val);    
      void InsertEdge(T v_val, T e_val);
  
      void PrintVertices();
      void PrintEdges(T v_val);
  
      void DeleteEdge(T v_val, T e_val);   
      void DeleteVertex(T val);
  
      void bfs();    
  
  private:
      Node&lt;T&gt; *head;
  
  };
  </code></pre>
  
  <p>// <strong>Graph.cpp</strong></p>
  
  <pre><code>#include "Graph.h"
  
  template &lt;class T&gt;
  void Graph&lt;T&gt;::InsertVertex(T val)
  {
      Node&lt;T&gt; *temp = new Node&lt;T&gt;(val);
  
      if(head == NULL) head = temp;
      else
      {
          Node&lt;T&gt; node = head;
  
          while(node-&gt;vertList != NULL)
              node = node-&gt;vertList;
  
          node-&gt;vertList = temp;
      }   
  }
  
  template &lt;class T&gt;
  void Graph&lt;T&gt;::InsertEdge(T v_val, T e_val)
  {
      if (head != NULL)
      {
          Node&lt;T&gt; *k = head;
          Node&lt;T&gt; *t = head;
          Node&lt;T&gt; *temp = new Node&lt;T&gt; (e_val);        
  
          while (t != NULL)
          {
              if (t-&gt;data == v_val)
              {
                  Node&lt;T&gt; *s = t;
  
                  while (s-&gt;next != NULL)
                      s = s-&gt;next;
  
                  s-&gt;next = temp;
  
                  while (k != NULL)
                  {
                      if(k-&gt;data == e_val) break;
  
                      k = k-&gt;vertList;
                  }
  
                  temp-&gt;vertList = k;
                  return;
              }
  
              t = t-&gt;vertList;
          } // end while loop        
      }
      else std::cout &lt;&lt; "Add first vertices to the graph" &lt;&lt; std::endl;
  }
  
  template &lt;class T&gt;
  void Graph&lt;T&gt;::PrintEdges(T v_val)
  {
      Node&lt;T&gt;* t = head;
  
      while (t != NULL)
      {
          if (t-&gt;data == v_val)
          {
              while (t-&gt;next != NULL)
              {
                  std::cout &lt;&lt; t-&gt;next-&gt;vertList-&gt;data &lt;&lt; "   ";
                  t = t-&gt;next;
              }
          }
          t = t-&gt;vertList;
      }
  }
  
  template &lt;class T&gt;
  void Graph&lt;T&gt;::PrintVertices()
  {
      Node&lt;T&gt;* t = head;
  
      while (t != NULL)
      {
          std::cout &lt;&lt; t-&gt;data &lt;&lt; "   ";
          t = t-&gt;vertList;
      }
  }
  </code></pre>