For my data-structures class, I am attempting to create a binary search tree template to be used to create an AVL tree. I've written a Generic_Tree template for the BST to inherit from, and before I jump into implementing the AVL tree I'm testing the BST member functions. Everything was compiling fine until I added the
BST insert() function. Now, I'm getting the following error message from my linker:
undefined reference to 'BST<void>::insert(int, void*)'
int main(int argc, char* argv[])
{
BST<void> The_Tree;
KEY nukey = NO_KEY;
int job = REDO;
bool run = true;
bool auto_print = display_intro();
while( run )
{
while( (job = menu( &nukey )) == REDO );
if( job == QUIT ) run = false;
else{
cout << "Not Quitting... ";
if( job == INSERT ) The_Tree.insert( nukey, NULL );//cout << "\nKEY: " << nukey << endl;//
if( job == DELETE ) cout << "\nKEY: " << nukey << endl;//The_Tree.remove( nukey );
if(( job == PRINT )||( auto_print )) ;//;The_Tree.display( INORDER );
}/* end job processing if-else */
}/* end run while */
return 0;
}/* end main func */
I'm use to c and havn't used classes or templates before (except for declaring instances of them). The whole syntax is mystifying to me, I would appreciate some elucidation.
undefined reference to 'BST<void>::insert(int, void*)'
That's probably the simplest problem to fix here: templates go into header files, not into the cpp files.
Remember, C++ has a separate compilation model: when the compiler compiles BST.cpp, it has no idea that somewhere out there exists a main.cpp that will try to instantiate BST<void>. As far as it knows, nobody needs this template, no code is compiled.
templates go into header files, not into the cpp files.
I have the templates and their member prototypes in header files and the member function implementations in cpp files. What I've pasted above under BST is the header, "...", and part of the cpp (sorry, I was trying to save space). I also didn't past above that in main I #include "BST.h". Is my problem with my g++ usage?