5

I implemented this function but it still give the following error and I can't figure out why..

/usr/include/c++/4.8/bits/stl_algo.h:6325: error: no match for call to '(Farm::killHeaviestAnimalOnFarm()::__lambda0) (Animal*&, Animal*&)' if (__comp(*__result, *__first)) ^

I searched some examples on the internet and I cannot see what's the difference between mine..

My .cpp file had the following killHeaviestAnimalOnFarm

Animal *Farm::killHeaviestAnimalOnFarm()
{
    auto minmax_widths = std::max_element(animals.begin(), animals.end(),
            [] (Animal const& lhs, Animal const& rhs) {
                return lhs.getWeight() < rhs.getWeight();
            });
}
9
  • 2
    What is "the following error"?
    – Kerrek SB
    Aug 15, 2015 at 23:47
  • 1
    Your function has no return statement and does not return void, so your program has undefined behaviour.
    – Kerrek SB
    Aug 15, 2015 at 23:48
  • 2
    I bet it's because animals is a std::vector<Animal*>. (I'm totally guessing here)
    – user123
    Aug 15, 2015 at 23:48
  • 2
    "And it is ineed a vector" It's a vector of pointers. That's what the error telling you - it's failing to pass pointers to your lambda function. Aug 15, 2015 at 23:57
  • 2
    How is this unclear -- your vector contains apples, and your lambda expects oranges. Change the lambda to like apples.
    – Kerrek SB
    Aug 15, 2015 at 23:59

1 Answer 1

10

If animals is vector<Animal*> then change your lambda function to :

       auto minmax_widths = std::max_element(animals.begin(), animals.end(),
            [] (Animal const * lhs, Animal const * rhs) {
            return lhs->getWeight() < rhs->getWeight();
    });

The alternative is to make animals a vector<Animal> in which case your lambda works as is.

Not the answer you're looking for? Browse other questions tagged or ask your own question.