6

I always get

undefined reference to `Graph::InsertVertex(std::string)'

if I compile my project! Any hints why he cant resolve this reference? (all Files are in the netbeans project folder)

// main.cpp

#include <cstdlib>
#include <string>
#include "Graph.h"

using namespace std;

int main(int argc, char** argv)
{
    Graph<string> *graph = new Graph<string>(); // <--- ERROR

    graph->InsertVertex("A");

    return 0;
}

// Node.h

#include <iostream>
#include "Graph.h"

template<class T> 
class Node
{   

friend class Graph;    

public:
    Node(T val)
    {
        this->data = val;
        this->vertList = NULL;
        this->next = NULL;
    }

    Node(const Node& orig);
    virtual ~Node();

private:
    T data;
    Node<T> *vertList;
    Node<T> *next;
    int status;

};

// Graph.h

#include <iostream>
#include "Node.h"

template <class T> 
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<T> *head;

};

// Graph.cpp

#include "Graph.h"

template <class T>
void Graph<T>::InsertVertex(T val)
{
    Node<T> *temp = new Node<T>(val);

    if(head == NULL) head = temp;
    else
    {
        Node<T> node = head;

        while(node->vertList != NULL)
            node = node->vertList;

        node->vertList = temp;
    }   
}

template <class T>
void Graph<T>::InsertEdge(T v_val, T e_val)
{
    if (head != NULL)
    {
        Node<T> *k = head;
        Node<T> *t = head;
        Node<T> *temp = new Node<T> (e_val);        

        while (t != NULL)
        {
            if (t->data == v_val)
            {
                Node<T> *s = t;

                while (s->next != NULL)
                    s = s->next;

                s->next = temp;

                while (k != NULL)
                {
                    if(k->data == e_val) break;

                    k = k->vertList;
                }

                temp->vertList = k;
                return;
            }

            t = t->vertList;
        } // end while loop        
    }
    else std::cout << "Add first vertices to the graph" << std::endl;
}

template <class T>
void Graph<T>::PrintEdges(T v_val)
{
    Node<T>* t = head;

    while (t != NULL)
    {
        if (t->data == v_val)
        {
            while (t->next != NULL)
            {
                std::cout << t->next->vertList->data << "   ";
                t = t->next;
            }
        }
        t = t->vertList;
    }
}

template <class T>
void Graph<T>::PrintVertices()
{
    Node<T>* t = head;

    while (t != NULL)
    {
        std::cout << t->data << "   ";
        t = t->vertList;
    }
}
emesx
  • 12,555
  • 10
  • 58
  • 91
leon22
  • 5,280
  • 19
  • 62
  • 100
  • 5
    You cannot put member function definitions of a class template in a `.cpp` file. – Andy Prowl Feb 16 '13 at 19:19
  • 3
    This's been asked at least five times this week. You should seriously improve your search-jutsu. :p – Simon G. Feb 16 '13 at 19:20
  • Search your own question title ["C++ undefined reference to template class method"](https://www.google.com/search?q=C%2B%2B+undefined+reference+to+template+class+method) for gobs of stackoverflow explanations. – Drew Dormann Feb 16 '13 at 19:27
  • What does `friend class Graph;` mean if `Graph` is not a `class`, but a `template`? Google for "C++ friend template" – Maciej Hehl Feb 16 '13 at 19:28

2 Answers2

18

Typically you want your template methods in the header, so they are compiled when needed. In case you really want to hide it in the implementation file, you have to explicitly instantiate the template in Graph.cpp like

template class  Graph<string>;

Since you have to do that for every type T you intend to use with Graph<T>, the point of the template class is somewhat defeated and you better put everything into the header

Fred Schoen
  • 1,372
  • 13
  • 18
  • It seems I'm not smart enough: I added now the definition from the cpp to the header file (extra definition or add it with braces to the function declaration) Give me an example for 1 function please! – leon22 Feb 16 '13 at 19:41
  • Note that it is not enough to actually instantiate an instance of the class to get the compiler to generate the appropriate methods. You need to use the `template class ClassName;` syntax to get it to work appropriately. You can't just stick `ClassName instance;` and expect the compiler to work out that it needs all the methods (which it should be able to do). You may want to do this to stop the compiler generating the same object over and over when the class template is included in multiple cpp files. – Eosis Feb 15 '17 at 13:01
7

You need to define member functions in a header file, because when instantiating a template, the compiler needs to have access to the implementation of the methods, to instantiate them with the template argument.

In your example:

template <class T> 
class Graph  {    
public:
    void InsertVertex(T val) {
        Node<T> *temp = new Node<T>(val);

        if(head == NULL) 
            head = temp;

        // ... 
    }

    // ...

private:
    Node<T> *head;
};
emesx
  • 12,555
  • 10
  • 58
  • 91