1

I am trying to sort a list of pointers to Pilot’s objects by the times they take to compete in a race. Times are computed through compete method in class Pilot. So I will be sorting them by a method rather than a class attribute. Classes involved in the problem:

class Pilot
{
    double compete(Race *race); // Computes the time the pilot takes to finish the race
}
class Race
{
    list<Pilot*> pilots; // Pilots competing

    void sortByTime();
}

I want to sort the list using a lambda expression. Here is my try:

void Race::sortByTimes()
{ 
    sort(pilots.begin(), pilots.end(), [this](Pilot *p1, Pilot *p2) -> bool
    {
        return p1->compete(this) < p2->compete(this);
    });
}

I got 5 compile errors in algorithm library:

Error C2784 'unknown.type std::operator - (std::move_iterator<_RanIt> &, const std::move_iterator<_RanIt2> &)' : could not deduce template argument for 'std::move_iterator<_RantIt> &' from 'std::_List_iterator<std::_List_val<std::_List_simple_types<PilotoOficial*>>>'

Two more C2784 errors in std:: reverse_iterator and std::_Revranit

Error C2676 binary : 'std::_List_iterator<std::_List_val<std::_List_simple_types<Pilot*>>>' : does not define this operator or a conversion to a type acceptable to the predefined operator

Error C2780 'void std::_Sort(_RanIt,_RanIt,_Diff,_Pr' : expects 4 arguments; 3 provided
1
  • You do not need the -> bool after your lambda arguments Commented Jun 3, 2015 at 17:02

1 Answer 1

3

Use std::list::sort. std::sort requires random-access iterators and you've given it bidirectional iterators. The other option would be to use a container like std::vector that has random-access iterators.

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

Comments

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.