Template Troubles

Posted by Nick Cash on Wed 16 Nov 2005 01:48 AM — 4 posts, 19,930 views.

USA #0
As I jump deeper into C++ I come into increasingly strange errors. Templates have been one of them. In my test programs (with simple templates) everything was fine, but I've taken some stuff out of my C++ book for Binary Tree's as a class, and class extentions, and I can't get it all to work correctly.

I've compiled the code into a library, and I also tried just adding the files to the test project I'm working on, but I keep getting these linking errors:

main.o: In function `ZN3BSTIiEC1Ev':

C:/programming projects/binary_tree_lib/test/main.cpp:(.text$_ZN3BSTIiED1Ev[BST<int>::~BST()]+0xd): undefined reference to `BTree<int>::~BTree()'
C:/programming projects/binary_tree_lib/test/main.cpp:(.text$_ZN3BSTIiEC1Ev[BST<int>::BST()]+0xd): undefined reference to `BTree<int>::BTree()'

Now, it all compiled fine, but when I try this in some function:

BST<int> tree;


It throws those errors. I've tried with several standard data types, all with the same errors. BST is a derived class from BTree. The constructors and destructors it is referencing to are thus:

template<class elemType>
BTree<elemType>::BTree()
{
  root = NULL;
} // end default constructor

template<class elemType>
BTree<elemType>::~BTree()
{
  Destroy(root);
} // end destructor


Both are declared in the class definition, and both are present in the code. Again, it compiled correctly, these are linking errors.

I'm not really sure where to go from here. Any help? Thanks :)
Amended on Wed 16 Nov 2005 01:56 AM by Nick Cash
USA #1
If I switch the declaration to add parentheses then it seems to work, making the instance declaration this:

   BST<int> tree();


Then, when I try this:

   tree.Insert(100);
   tree.Insert(110);
   tree.Insert(50);


I get this:

main.cpp: In function `int main()':
main.cpp:12: error: request for member `Insert' in `tree', which is of non-class type `BST<int> ()()'
main.cpp:13: error: request for member `Insert' in `tree', which is of non-class type `BST<int> ()()'
main.cpp:14: error: request for member `Insert' in `tree', which is of non-class type `BST<int> ()()'

I think perhaps the declaring of the BST class is wrong? It is currently:

template<class Type>
class BST: public BTree<Type>


This problem may be spawned by the previous problem. I'm not sure why I would have to add the parentheses after the instance declaration.
Australia Forum Administrator #2
Quote:

I've compiled the code into a library ...


I think this could be your problem. By their nature templates cannot be compiled into a library, or even a different file in your current projbect.

Since the template code is needed when you instantiate a particular type of thing (eg. BST<int> in your case) then then template "include" file has to be in that source file, the one where you are doing it.
USA #3
I'm not currentlu working off the library currently, not have I been for those tests (though tests with the library yield the same results).

Both of the includes were included, but for the sake of trying I put all of their contents in the main file. Same thing.

I've taken all code from all of the files and thrown it into the main file as well. Piece by piece, I got it to work when the instance did nothing but create itself and destroy itself. However, thats not very useful now is it? :P

I tried the Insert method, and I got this:

main.cpp:115: error: passing `const BST<int>' as `this' argument of `void BTree<Type>::SetRoot(nodeType<Type>*) [with Type = int]' discards qualifiers


That error is invoked by this:

  nodeType<Type> *new_node = NULL; 
  new_node = new nodeType<Type>;

  if ( this->root == NULL )
   this->SetRoot(new_node); // this line being the culprit


which is inside the Insert method. I'm not sure exactly what it means (the error), as it looks like I'm passing it right (to me anyway).

Oh, SetRoot is this:

    void SetRoot( nodeType<Type>* r )
    {
      if ( r != NULL )
       root = r;
    }


I think I may stay away from templates if they prove to be this troublesome, though I have the feeling something will click and all will be well.