method sort () gives me unexpected error, in C ++ (QT)

0

I have a list that stores objects like AIRCRAFT and I want to sort it by one of its float parameters downwards.

In a class called airport I have a floating fleet static list, in which, in a part of the program I fill all the data.

Code of the function that I imagine is wrong:

bool Avion::mayor(const Avion&A, const Avion&B)
{
    return A.porcientoCapacidadOcupada > B.porcientoCapacidadOcupada;
}
void Avion::ordenarFlota()
{
Aeropuerto *obj =new Aeropuerto();
    obj->flotaAviones.sort(mayor);
}

The error that throws me is > D

  

: \ Qt \ Tools \ mingw530_32 \ i686-w64-mingw32 \ include \ c ++ \ bits \ list.tcc: 385:

error: must use '.*' or '->*' to call pointer-to-member function in '__comp (...)', e.g. '(... ->* __comp) (...)'
        if (__comp(*__first2, *__first1))
              ^

I do not know what I'm doing wrong.

    
asked by HOLDTHEDOOR 09.10.2018 в 14:41
source

1 answer

0

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
    
answered by 09.10.2018 в 19:54