Welcome Guest [Log In] [Register]
Welcome to UCR CS 14 Klefstad.

You're currently viewing our forum as a guest. This means you are limited to certain areas of the board and there are some features you can't use. If you join our community, you'll be able to access member-only sections, and use many member-only features such as customizing your profile, sending personal messages, and voting in polls. Registration is simple, fast, and completely free.


Join our community!


If you're already a member please log in to your account to access all of our features:

Username:   Password:
Add Reply
Template Help
Topic Started: Nov 15 2013, 03:41 PM (127 Views)
Deleted User
Deleted User

I'm looking at every template tutorial on the internet, trying to understand how to convert to templates. And now I'm getting undefined reference errors in my main function. I'm putting:

template <typename KeyType, typename ElementType>

in front of every function and

<KeyType, ElementType>

after every instance of TreeNode and BinarySearchTree except in the constructors and deconstructors. What am I doing wrong?
Quote Post Goto Top
 
Deleted User
Deleted User

Here's and example of where template stuff needs to go when templating.
Code:
 
.
.
.
template <typename KeyType, typename ElementType>
class BinarySearchTree
{
private:
TreeNode<KeyType, ElementType>* head;

public:
.
.
.
}

template <typename KeyType, typename ElementType>
BinarySearchTree<KeyType, ElementType>::BinarySearchTree()
{}
.
.
.
template<typename KeyType, typename ElementType>
void BinarySearchTree<KeyType, ElementType>::print(ostream& out)
{
TreeNode<KeyType, ElementType>::print();
}
.
.
.
template<typename KeyType, typename ElementType>
void insertAllWords(BinarySearchTree<KeyType, ElementType> &BSTree, istream& in)
{
.
.
.
}


Basically, if a struct or class has been templated, then it needs to be passed template parameters whenever an instance of that class or struct is made. The scope resolution operator (::) needs them as well. This is why, in print and when implementing the member functions, the template parameters are passed in. Klefstad went over a way to reduce the need for all that (it can get really messy as you can probably tell).

The way to simplify this is to declare the TreeNode class/struct in the private section of the BinarySearchTree declaration and to implement all functions in their class declaration. This makes the class declaration look messy, but will save you a lot of time typing out all those template parameters.
Quote Post Goto Top
 
Deleted User
Deleted User

If you do it that way, you have to provide instantiations for every type that you want to use at the bottom of the .cpp such as

template class BinarySearchTree<std::string, std::string>;
template class BinarySearchTree<int, int>;

which is why you just include the .cpp file at the bottom of the .h
Quote Post Goto Top
 
1 user reading this topic (1 Guest and 0 Anonymous)
« Previous Topic · Homework 6 · Next Topic »
Add Reply