[Home] [Downloads] [Search] [Help/forum]

Gammon Forum

See www.mushclient.com/spam for dealing with forum spam. Please read the MUSHclient FAQ!

[Folder]  Entire forum
-> [Folder]  Programming
. -> [Folder]  General
. . -> [Subject]  Template Troubles
Home  |  Users  |  Search  |  FAQ
Username:
Register forum user name
Password:
Forgotten password?

Template Troubles

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page


Posted by Nick Cash   USA  (626 posts)  [Biography] bio
Date Wed 16 Nov 2005 01:48 AM (UTC)

Amended on Wed 16 Nov 2005 01:56 AM (UTC) by Nick Cash

Message
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 :)

~Nick Cash
http://www.nick-cash.com
[Go to top] top

Posted by Nick Cash   USA  (626 posts)  [Biography] bio
Date Reply #1 on Wed 16 Nov 2005 01:56 AM (UTC)
Message
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.

~Nick Cash
http://www.nick-cash.com
[Go to top] top

Posted by Nick Gammon   Australia  (21,322 posts)  [Biography] bio   Forum Administrator
Date Reply #2 on Wed 16 Nov 2005 02:26 AM (UTC)
Message
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.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Nick Cash   USA  (626 posts)  [Biography] bio
Date Reply #3 on Wed 16 Nov 2005 03:52 AM (UTC)

Amended on Wed 16 Nov 2005 04:05 AM (UTC) by Nick Cash

Message
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.

~Nick Cash
http://www.nick-cash.com
[Go to top] top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


8,104 views.

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page

Go to topic:           Search the forum


[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.

[Home]


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at FutureQuest]