Phase 2: TreeStringSet
to TreeSet<T>
Overview
In this part you will turn your TreeStringSet
class into a TreeSet<T>
class template.
- Begin with working code. In the previous part, you should have code that runs correctly and has no compiler warnings. This way you will know that any errors you encounter at this stage are due to the conversion process to turn your tree class into a class template.
- Use the
IntVector
→Vector<T>
example as a guidepost. If you're not sure about the transformation process, the code from the lesson on templates will help! - Be systematic with compiler errors. Start with the first error (and the first file that belongs to you) and look there for the error.
Your Task…
TreeStringSet
to TreeSet<T>
In this part, you'll transform the TreeStringSet
class into a TreeSet
class template. Here's how…
When turning your class into a template:
- In
treeset.hpp
,- Put
template <typename T>
in front of the class declaration. - Put
template <typename T>
in front your declaration ofoperator<<
, and change it to take aTreeSet<T>&
.
- Put
- In
treeset-private.hpp
,- Every function needs
template <typename T>
in front of it. - Every function that said
TreeStringSet::
in front now needsTreeSet<T>::
- Every place that needs a type and uses a type from inside
TreeSet<T>
needstypename
in front of it; thus it'stypename TreeSet<T>::const_iterator
notTreeSet<T>::const_iterator
as a return type.typename TreeSet<T>::reference
notTreeSet<T>::reference
as a return type.- Don't go wild adding
typename
, if you forget it somewhere, the compiler will tell you where it's needed!
- Every function needs
- In both
treeset.hpp
andtreeset-private.hpp
,- Be sure to change all uses of
std::string
(or juststring
if you opened thestd
namespace) to be the type parameterT
.
- Be sure to change all uses of
Fix operator<<
Transform the global operator<<
function for TreeStringSet
s to a function template that works on TreeSet<T>
s.
Fix Your Tests
Modify your tests to use TreeSet<std::string>
rather than TreeStringSet
.
At this point, you can make sure everything works.
Add More Tests
Your TreeSet
class template should be able to work on other types besides std::string
. For example,
TreeSet<double>
TreeSet<int>
Helpful Hints
Using the lesson materials
In the lesson materials, there is a good example of the kind of transformation you need to do, where it turns IntVector
into Vector<T>
. When you're not sure how to do something, you can use that code as a guide.
Declaring TreeSet<T>::Iterator
member functions
Don't forget that even the member functions for TreeSet<T>::Iterator
are templates (because of the T
type parameter)!
Otherwise, you wouldn't have a type variable to give to TreeSet
when defining the function, right?
Adding typename
When a function returns a templated nested class, we find outselves in one of those situations mentioned in the lesson materials where we need to use typename
.
Specifically, C++ will needs you to explicitly prefix return types of the form Foo<T>::Bar
with the keyword typename
(to tell it that Bar
is a type not a value).
I'm still a bit confused about where I should add
typename
.
You can either just wait for a compiler error message telling you where to add typename
, or you can look the lesson materials for the transformation from IntVector
to Vector<T>
to see how its done (and read an explanation on why we have to do this).
This issue will surely come up with functions like
begin()
andend()
. So watch out!
(When logged in, completion status appears here.)