0

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.

2
  • 2
    Did you forget to add const-ness to your Event &a and Event &b? Commented Dec 20, 2011 at 20:45
  • Event::getBeginTime() should be declared const. If it's not your code, maybe you could get the author to fix it. If it IS your code, FIX IT! 8v) Commented Dec 20, 2011 at 20:46

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I was trying to follow an example that was not correct. I should have overloaded the < operator, but the compiler said I needed a third argument to do that and I could not figure out what that would be.
0

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()))); 

}

2 Comments

I tried that, the compiler kept telling me I needed a third arg
The answer that got deleted said to make a function basically like what you have only name it compare and remove the operator overload all together. then the vector sort is std::sort(eventVentor.begin(), eventVector.end, compare). That made sense but I got frustrated and wrote a bubble sort that seems to be working. Thanks for the help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.