Question

This is the class that contains the overloaded function operator:

template < typename KeyType=int >
class Less {
  public:
    bool operator()(const KeyType &a, const KeyType &b) const { return a < b; }
  };

How can I use this? This class is specified in a header that contains a class specification for a heap ADT as well. I am trying to use it in one of my heap ADT's member functions, and am not sure of the syntax. I assumed it would be as follows:

if(Less<KeyType>::(param1, param2)){
...

But the compiler gives me an error: expected unqualified-id before '(' token

It DOES work like this:

if(Less<KeyType>::operator()(param1, param2)){
...

But there has to be a way to use it in another way that is less cluttered. If I wanted it to look like this, I wouldn't have overloaded an operator in the first place and would have just made it a typical function.

I've tried doing some research on this question before asking here, but it's a little hard to find an answer to something this specific. My searches keep leading me to different topics.

Was it helpful?

Solution

This operator is a non-static member function. So you need to create an object of type Less to call it. For example

if ( Less<>()( param1, param2 ) ) { /*...*/ }

Or

if ( Less<SomeType>()( param1, param2 ) ) { /*...*/ }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top