You are calling the sort
from a list of the STL, your signature is as follows:
template< class Compare >
void sort( Compare comp );
That is, wait for a comparator that you will use to compare the elements, this comparator can be:
-
A functor : you will need to create a structure that has the overloaded parenthesis operator and pass an instance to the function sort
.
struct ordenaAviones
{
bool operator()(const Avion&A, const Avion&B)
{
return A.porcientoCapacidadOcupada > B.porcientoCapacidadOcupada;
}
};
void Avion::ordenarFlota()
{
Aeropuerto *obj =new Aeropuerto();
obj->flotaAviones.sort(ordenaAviones{});
}
-
One lambda : You can use the lambda directly as a parameter of sort
:
void Avion::ordenarFlota()
{
Aeropuerto *obj =new Aeropuerto();
obj->flotaAviones.sort([](const Avion&A, const Avion&B)
{ return A.porcientoCapacidadOcupada > B.porcientoCapacidadOcupada; });
}
-
A free function: a free function is a function that does not belong to any class.
bool ordenaAviones(const Avion&A, const Avion&B)
{
return A.porcientoCapacidadOcupada > B.porcientoCapacidadOcupada;
}
void Avion::ordenarFlota()
{
Aeropuerto *obj =new Aeropuerto();
obj->flotaAviones.sort(ordenaAviones);
}
The reason why it worked for you when you passed bool Avion::mayor(const Avion&, const Avion&)
to static function is that the static case functions (like free functions) do not need an object instance to be called, that the lack of instance is precisely the error you were receiving because you have passed it as a parameter to sort
bool(Avion::*)(const Avion&, const Avion&)
A member function pointer of Avion
that returns bool
and receives two constant references to Avion
. To be able to invoke a class member function pointer, you need a pointer or instance of the class and call the operator .*
or ->*
:
struct S { void f(){} };
using puntero = void(S::*)();
puntero p = &S::f;
S s;
S *ps = &s;
(s.*p)(); // Llama a S::f
(ps->*p)(); // Llama a S::f