CS 70

Phase 2: TreeStringSet to TreeSet<T>

Overview

In this part you will turn your TreeStringSet class into a TreeSet<T> class template.

  1. 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.
  2. Use the IntVectorVector<T> example as a guidepost. If you're not sure about the transformation process, the code from the lesson on templates will help!
  3. 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 of operator<<, and change it to take a TreeSet<T>&.
  • In treeset-private.hpp,
    • Every function needs template <typename T> in front of it.
    • Every function that said TreeStringSet:: in front now needs TreeSet<T>::
  • In both treeset.hpp and treeset-private.hpp,
    • Be sure to change all uses of std::string (or just string if you opened the std namespace) to be the type parameter T.

(Note that because we're using C++ 20, we don't need to prefix TreeSet<T>::const_ConstIter with typename. If you do, it won't hurt anything (and will let your code compile on older compilers), but it's not necessary, and adding typename in the wrong place can cause very confusing error messages!)

Fix operator<<

Transform the global operator<< function for TreeStringSets 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>

You don't need to go wild repeating every test with different types, but you should do enough testing to be sure that there's nothing string-specific in your implementation.

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>::ConstIter member functions

Don't forget that even the member functions for TreeSet<T>::ConstIter 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 ourselves 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).

  • Hedgehog speaking

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

  • LHS Cow speaking

    This issue will surely come up with functions like begin() and end(). So watch out!

To Complete This Part of the Assignment…

You'll know you're done with this part of the assignment when you've done all of the following:

(When logged in, completion status appears here.)