0

Sorry new poster here and really couldn't phrase my question particularly well. Also apologize for any breaches in etiquette ahead of time. I am working on templatizing a class. I pass two tests on it but I fail the third test: This class provides a minimal set of operations and, as such, represents a test of whether your MiniMax class requires its data to support more functions than are absolutely necessary.

template <class Data>
void MiniMax<Data>::observe (const Data& t)
{
  if (count == 0)
    min = max = t;
  else
    {
      if (t < min)
    min = t;
      if (t > max)
    max = t;
    }
  ++count;
}

It fails at the line if(t>max) with no match for operator> during compilation. How do I alter my template so it doesn't require > to be implemented in the user-defined class? In this assignment, I can only edit the template and not any of the test drivers.

ntalbs
  • 28,700
  • 8
  • 66
  • 83
Addy
  • 17
  • 2

1 Answers1

1

As @Anthony Sottile said in the comments the simplest way to do this would be to switch the placement of the operands and change the operator, changing t > max to max < t. This reuses the operator and still does the same thing.

After changing it your code would look like this:

if (count == 0)
   min = max = t;
else
{
    if (t < min)
        min = t;
    if (max < t) // <-- Difference Here
        max = t;
}
++count;
phantom
  • 3,292
  • 13
  • 21
  • Thanks! That does work, submitting now we'll see if the autograder likes it. This seems like an arbitrarily crumby thing to do in an assignment so I'm not sure it's exactly what the professor is looking for. Any other way to do it that you can think of? – Addy May 28 '16 at 21:28
  • Not with a single operator. You could do `!(t < max) && t != max` but that still uses two operators. If you don't mind setting `max` to `t` when they are equal you could only do `!(t < max)` but that changes what your program actually does. – phantom May 28 '16 at 21:33
  • That does work but then it fails the other tests which specifically look for the > operator. Is there anyway to split the three tests (=,<,>) somehow? – Addy May 28 '16 at 21:35
  • You could specialize the template function depending on whether `Data` has an `operator >` using something like [this](http://stackoverflow.com/questions/257288/is-it-possible-to-write-a-c-template-to-check-for-a-functions-existence). – phantom May 28 '16 at 21:47
  • Apparently swapping >with < was what he was looking for. Thanks again! – Addy May 28 '16 at 22:43