| 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: |
| Template Help | |
|---|---|
| Tweet Topic Started: Nov 15 2013, 03:41 PM (127 Views) | |
| Deleted User | Nov 15 2013, 03:41 PM Post #1 |
|
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? |
|
|
| Deleted User | Nov 15 2013, 05:29 PM Post #2 |
|
Deleted User
|
Here's and example of where template stuff needs to go when templating.
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. |
|
|
| Deleted User | Nov 15 2013, 08:29 PM Post #3 |
|
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 |
|
|
| 1 user reading this topic (1 Guest and 0 Anonymous) | |
| « Previous Topic · Homework 6 · Next Topic » |





12:16 PM Jul 11