0

I'm trying to make this code work, and I was given a binary tree .cpp and header, with objects Billete. How can I make a function to call another static function, to print all of its Billete? I am trying to make it work but I don't seem to understand function pointers. Thank you so much!

arbolABB.h

class ArbolABB{
  private:
   //// Clase local de Lista para Nodo de ArbolBinario:
   class Nodo {
     public:
      // Constructor:
      Nodo(Billete dat, Nodo *izq=NULL, Nodo *der=NULL) :
        dato(dat), izquierdo(izq), derecho(der) {}
      // Miembros:
      Billete dato;
      Nodo *izquierdo;
      Nodo *derecho;
   };

   // Punteros de la lista, para cabeza y nodo actual:
   Nodo *raiz;
   Nodo *actual;
   int contador;
   int altura;

  public:
   void InOrden(void (*func)(Billete&) , Nodo *nodo=NULL, bool r=true);
   //etc

arbolABB.cpp

// Recorrido de árbol en inorden, aplicamos la función func, que tiene
// el prototipo:
// void func(Billete&);
void ArbolABB::InOrden(void (*func)(Billete&) , Nodo *nodo, bool r)
{
   if(r) nodo = raiz;
   if(nodo->izquierdo) InOrden(func, nodo->izquierdo, false);
   func(nodo->dato);
   if(nodo->derecho) InOrden(func, nodo->derecho, false);
}

void ArbolABB::mostrar(){
    InOrden(&Billete::mostrar); //here is where I'm failing to make it work
    }

Billete.cpp

void Billete::mostrar(){
    cout << "________________________________________________________" << endl;
    cout << "PNR: " << PNR << "\t\t\t\t\t\t|"<< endl;
    cout << "Nombre: " << nombre << "\t\t\t\t|"<< endl;
    cout << "DNI: " << DNI << "\t\t\t\t\t\t|"<<endl;
    cout << "Vuelo: " << vuelo.idVuelo << "\t Origen: " << vuelo.origen << "\t Destino: " << vuelo.destino << "\t\t|" <<endl;
    cout << "Fecha: " << vuelo.fecha << "\t Hora de salida: " << vuelo.hora << "\t\t\t|"<<endl;
    cout << "Asiento: " << asiento << "\t\t\t\t\t\t|"<<endl;
    cout << "________________________________________________________|" << endl;
    if (!equipaje.esVacia()){
        cout << "\nListado de equipaje: \n"<< "--------------------"<< endl;
        equipaje.mostrar();
    }else{
        cout << "Pasajero sin equipaje." << endl;
    }
    cout << "________________________________________________________\n\n\n" << endl;
}
Jaime
  • 1
  • 1
  • 1
    Please read about [mcve], and quesitons about a compiler error should include the compiler error in the quesiton. Though, its rather clear what the issue is, a member function is incompatible with a free function. YOu need an object to call the former – 463035818_is_not_an_ai Dec 13 '22 at 10:44
  • There are also many related SO posts for this. Did you try searching the web? – Jason Dec 13 '22 at 10:44
  • 2
    `InOrden(&Billete::mostrar)` causes a compiler error because `Billete::mostrar` is a member function. See [How can I pass a member function where a free function is expected?](https://stackoverflow.com/questions/12662891/how-can-i-pass-a-member-function-where-a-free-function-is-expected) for possible solutions. – kotatsuyaki Dec 13 '22 at 10:56
  • Question has already been asked on the site. But you want to take a look at pointers to methods: https://isocpp.org/wiki/faq/pointers-to-members – Laurent Jospin Dec 13 '22 at 10:56
  • And if you really need a function, and not a method, you can use mem_fn to get the pointer to the actual function: https://en.cppreference.com/w/cpp/utility/functional/mem_fn – Laurent Jospin Dec 13 '22 at 10:58

0 Answers0