Reputation: 1137
I am trying to follow a link where a someone is trying to simplify the C++ sort reference http://www.cplusplus.com/forum/beginner/4817/ however I can not get the bool
operator function to compile the way he has it.
I have vector of event objects. I want to sort the vector based on the event begin time. If the begin times were numbers, this would be easier but they are strings so I had to write functions to convert to uint64_t
, all of my code up to this sort attempt is working as it should. Here is the code I am trying to get to work:
The bool function:
bool EWriter:: operator () ( Event &a, Event &b){
return (stringToTime(stringReturnWrap(a.getBeginTime())) < stringToTime(stringReturnWrap(b.getBeginTime())));
}
This code compiles but I can not figure out how to give it a name, and therefore I cannot reference it in the sort. Also, I would rather overload the <
operator but I keep getting an error it needs a third argument.
Here is my sort:
sort(events->begin(), events->end(), someFunctionName);??
Slightly unrelated is that I know I am supposed to use const in the args but I can not call the functions of the Event class if I have them implemented.
Upvotes: 0
Views: 272
Reputation: 2363
You are overloading the parentheses operator:
bool EWriter:: operator () ( Event &a, Event &b){
return (stringToTime(stringReturnWrap(a.getBeginTime())) < stringToTime(stringReturnWrap(b.getBeginTime())));
}
Try with:
bool EWriter:: operator <( Event &a, Event &b){
return (stringToTime(stringReturnWrap(a.getBeginTime())) < stringToTime(stringReturnWrap(b.getBeginTime())));
}
Upvotes: 0
Reputation: 63745
By overloading operator()
, you have made EWriter
a functor.
Just pass it an instance of EWriter
.
If EWriter
has a default constructor, you can use:
sort(events->begin(), events->end(), EWriter());
Or pass it an EWriter
that already exists.
Upvotes: 2