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 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!
  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 of 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_iterator 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 type nested inside a class template, such as TreeSet<T>::const_iterator, the lesson materials explain that C++ may need the keyword typename to know that the name is a type and not a value.

Because we compile with -std=c++20, you can usually leave it off: C++20 assumes a type in places, like a function's return type, where only a type makes sense. (Older C++ standards, and some of the lesson's examples, insist on typename there.)

  • Hedgehog speaking

    I'm still a bit confused about where I should add typename.

If the compiler ever does say missing 'typename', add it exactly where it points, or look at the lesson materials for the transformation from IntVector to Vector<T> to see how it's done (and read an explanation of why it's sometimes needed).

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