template<class T>
class bintree
    bin_node<T> *root;
    void insert(const T);
    void insert(const T, bin_node<T>*);

#include "bintree.h"

template<typename T>
void bintree<T>::insert(const T input)
    insert(input, root);

//tstatcontain is a non-template class-class
class tstatcontain
    string tname;
    int wins, loses, games, champ, years_playing, playoffs;
    {tname="empty team name"; wins=0; loses=0; games=0; champ=0;
        years_playing=0; playoffs=0;}
    //constructor assumes an input of format from *.csv:
        //tname, years_playing, games, wins, lose, playoffs, champ
    tstatcontain(char* in);
    void display(void);
    string get_tname () { return tname; }
    int get_wins () { return wins; }
    int get_loses () { return loses; }
    int get_games () { return games; }
    int get_champ () { return champ; }
    int get_yearsplaying () { return years_playing; }
    int get_playoffs () { return playoffs; }

int main(int argc, char* argv)
    ifstream idatabase("database.csv");
    bintree<tstatcontain> teamstats;
    //parse file and build tree
        char inputline[256];
        idatabase.getline(inputline, 256);
        tstatcontain in(inputline);
//some code to use tree here
    return 0;

Hi all,

I have been writing this code for a project and everything seems to be going great as it compiles normally, but when the compiler begins to link everything I get an error: Release\obj\main.o:main.cpp:(.text+0x240)||undefined reference to `bintree<tstatcontain>::insert(tstatcontain)'| The problem seem to lie in the implementation of the template class bintree but I dont know how to fix it. All my reference seems to point out that I have used the syntax for implementing template classes correctly. Is there anything else I should know in order to resolve this problem (why is this happening?)?

Thanks in advance. :)

Practically, when working with templates, it is necessary to have the template definitions (ie the implementation of template member functions) in the header file. It is not possible (with most compilers) to compile templates separately.

The reason is that, when using templates, the compiler needs to see the definition (ie implementation) of template functions at the point the functions are used rather than just a declaration (a prototype).

In other words, #include "bintree.cc" in "bintree.h", not the other way around. I normally would rename your bintree.cc to bintree.tem (or some other extension) to avoid the temptation to compile it separately.

There are some compilers that allow you to have separate definition and instantiation of templates, but the hoops you have to go through to achieve this are compiler dependent.

The next problem you will find is that you have no implementation of bintree<T>::insert(const T, bin_node<T>*) ;)

This article has been dead for over six months. Start a new discussion instead.