views:

293

answers:

3

I'm trying to sort a list (part of a class) in descending containg items of a struct but it doesn't compile(error: no match for 'operator-' in '__last - __first'):

sort(Result.poly.begin(), Result.poly.end(), SortDescending());

And here's SortDescending:

 struct SortDescending
{
    bool operator()(const term& t1, const term& t2)
    { 
        return t2.pow < t1.pow; 
    }
};

Can anyone tell me what's wrong?

EDIT It works now thanks to Glen and everyone else!

Thanks!

+3  A: 

std::list has a built-in sort method that you need to use since std::sort only works with random access iterators, whereas std::list::iterator merely belongs to the bidirectional iterator class of iterators.

But you don’t need to write your own comparer for that, simply use std::greater<T>:

Result.poly.sort(comparer, SortDescending());

Also, your operator () should be marked const.

struct SortDescending
{
    bool operator()(const term& t1, const term& t2) const
    { 
        return t2.pow < t1.pow; 
    }
};

Finally, you don’t need to write your own comparer for that, simply use std::greater<T> (located in the standard header <functional>):

Result.poly.sort(greater<term>());
Konrad Rudolph
No this isn`t it, there's nothing in the standard that says that this needs to be const. If you look at the error message it seems like `operator -` is missing for the input iterators.
Andreas Brinck
It would make a better answer if it was reordered (const-ness is a side-issue here).
visitor
still doesn't work either with my own comparer or using greater() it still gives a bunch of errors
Vlad
@Andreas: my concern was that a temporary object gets passed into the `sort` function. I had forgotten that the comparer is passed by value and since temporaries cannot be bound to non-`const` references this would have required the function to be `const`.
Konrad Rudolph
@visitor: You’re right, I’ve changed that.
Konrad Rudolph
@Vlad: *what* errors? This code should work. Did you include the header `<functional>` for `std::greater`?
Konrad Rudolph
`std::greater` will only work if `operator>` is overloaded for `term` which probably isn't the case here.
UncleBens
@UncleBens: true, true.
Konrad Rudolph
+4  A: 

It seems like the iterator types for Result.poly is missing operator -. std::sort doesn't work with std::list change to Result.poly.sort

Andreas Brinck
but i don't know how to overload correctly the less operator for my class
Vlad
@Vlad you can call this by `Result.poly.sort(SortDescending())`, no need for `operator <`.
Andreas Brinck
@Konrad I think he was talking about `operator <` and had missed the fact that there's a version of `std::ist::sort` that takes a predicate.
Andreas Brinck
+8  A: 

The standard algorithm std::sort requires random access iterators, which std::list<>::iterators are not (list iterators are bidirectional iterators).

You should use the std::list<>::sort member function.

David Rodríguez - dribeas
but i don't know how to overload correctly the less operator for my class
Vlad
@Vlad, you don't need to overload anything. `Result.poly.sort(SortDescending());` should work just fine.
Glen
The `operator ()` in your comparer should *still* be marked `const` as it doesn’t modify any members.
Konrad Rudolph
yes i did that also Konrad, thanks!
Vlad