It goes where the /*goes here comment*/ was before; you need the begin, you need the end, and then you need something that, if you put () after it compares the things.
Printable View
It goes where the /*goes here comment*/ was before; you need the begin, you need the end, and then you need something that, if you put () after it compares the things.
Would something like this work.
Code:std::sort(myElections->at(i)->myCandidates->begin(), myElections->at(i)->myCandidates->end(), Election::Candidate::operator ());
Didn't we start here? The third parameter needs to be a function object. I.e., it needs to be something that has an operator(), not an operator() itself. I.e., it needs to be an object of type Election::Candidate, so that when operator () is applied to it, the Election::Candidate::operator() you defined comes out.
(I mean, maybe operator() will work if it can accept a function pointer in the third spot in place of a function object, but I've never tried.)
What would I place in the third parameter exactly or what pointer could I get rid of to make it easier.
If it were me, I would have called my sorting function
made it a free function (i.e., not in a class), made it a friend if necessary, and not used the third parameter to sort at all.Code:bool operator<(const Election::Candidate*& lhs , const Election::Candidate*& rhs)
If for some reason I'm not allowed to overload that operator, I'd make it its own object:
and just used Bob for the third parameter.Code:class Bob {
operator () {const Election::Candidate*& lhs , const Election::Candidate*& rhs) //etc
}
Where would I overload the operator< at. Outside or inside the class, and If so how does it know to use that operator overload.
It knows because that's what it will try as a default. If you have a vector container of some type (foo for example) and call the default version of the sort function the compiler will try to use/find the operator< that works with foo objects. If it can't find one then the compiler will complain and you'll get a compiler error.
As mentioned, you can create a free function version of operator< (not inside the class) that should take two const references to your object type and does a comparison of those two objects returning a true/false boolean value. Then you'd just call the default version of sort and the compiler should take care of things for you at compile time.
The compiler gives me this final error
'operator <' must have at least one formal parameter of class typeCode:bool operator<(const View::Election::Candidate*& lhs , const View::Election::Candidate*& rhs){
return *lhs->myTotalNumberOfVotes < *rhs->myTotalNumberOfVotes;
}
I was afraid of that. (Reason 347 not to use pointers -- you can't overload.) Then you'll have to go with curtain 2 and a function object.
which pointer messes it up. I can change it.
The vector-of that we're trying to sort. Since we're trying to sort pointers, the compiler gives us a frowny face, because pointers are built-in types. If we were trying to sort Election::Candidates themselves, we could define operator<.
Those are the only pointers that I have to have for the assignment. Would having the class that just has the operator< work with the pointers.
Then you'll have to go with class Bob and the function object.
Is that what I would have to do.Code:#ifndef COMPARATOR_H
#define COMPARATOR_H
#include "View.h"
class Comparator{
bool operator () (const View::Election::Candidate*& lhs , const View::Election::Candidate*& rhs){
return *lhs->myTotalNumberOfVotes < *rhs->myTotalNumberOfVotes;
}
};
#endif
Does your assignment require you to use a billion pointers? If not, I think your time would be better spent doing it properly without all the pointers, rather than trying to fit a square peg into a round hole...