Call dynamic functions from static function?

4

I'm having a very silly problem, and I do not know what else to give it, I've tried creating instances but something will be wrong.

I have a static function that collects the value of an external window, and depends on the value of that function, I need you to call other functions.

TareaView.h
class PLUGIN_TAR_EXPORT TareaView
      : public BlForm
      , public Ui::TareaBase
{
   Q_OBJECT

public:
   TareaView(BlMainCompany *, QWidget *);
  ...

   static void recogerValor(QString);
   void avisarAsignado();

....
}
TareaView.cpp (static function)
void TareaView::recogerValor(QString v){

    if(v==""){
        g_s->msgError("Nada seleccionado");
        avisarAsignado();
    }
    if(v=="0"){
        g_s->msgError("Madera");
    }
    if(v=="1"){
        g_s->msgError("Metaclilato");
    }
    if(v=="2"){
        g_s->msgError("Ambos");
    }
}

(dynamic function)

void TareaView::avisarAsignado(){

    m_enviando_email_al_asignado = true;
    QString nombre_from = mui_list->dbValue("empleado_solicitante");
    QString email_from = mui_list->dbValue("email_empleado_solicitante");
    QStringList nombres_to;
    QStringList emails_to;
    QString id_empleado_asignado = mui_list->dbValue("id_empleado");

    avisar(nombre_from, email_from, nombres_to, emails_to);

}

ERROR:

tareaview.cpp. error: cannot call member function ‘void TareaView::avisarAsignado()’ without object
     avisarAsignado();

Following your recommendations , I have not been able to solve the problem.

Since I do not know exactly where or how to create the instance. I through a static function "collectValue" that is in TaskView.cpp I collect the value of the number of checkboxs selected in TaskViewEquipment, and with the action of the accept button of the window, I pass the collected value to "collectValue" of Tareaview.

From TareaView, I call the window like this:

TareaViewEquipo *bud = new TareaViewEquipo((BlMainCompany *) mainCompany(), this); 
bud->show();

The window opens, I select the checkboxes, and when clicking on accept, before close (), I send the value to TaskView in the following way, to the static function.

TareaView::recogerValor(QString(valor),TareaView &tv); 

And now he says:

tareaview.cpp:998: error: prototype for ‘void TareaView::recogerValor(QString, TareaView&)’ does not match any in class ‘TareaView’ void TareaView::recogerValor(QString v, TareaView &tv){ ^ 

Y:

tareaview.h:117: error: candidate is: static void TareaView::recogerValor(QString, TareaView)
static void recogerValor(QString, TareaView);
            ^

Deputy Functions:

TareaView.h
class PLUGIN_TAR_EXPORT TareaView
      : public BlForm
      , public Ui::TareaBase
{
   Q_OBJECT

public:
   TareaView(BlMainCompany *, QWidget *);

   static void recogerValor(QString, TareaView);
   void avisarAsignado();
...

protected:
...
TareaView.cpp Where I call the window:
TareaViewEquipo *bud = new TareaViewEquipo((BlMainCompany *) mainCompany(), this);
        bud->show();
TareaViewEquipo.cpp (window)
void TareaViewEquipo::on_mui_aceptar_clicked()
{
    QString valor;
    valor=pasarvalor();
    TareaView::recogerValor(QString(valor));
    close();
}
TareaView.cpp (Statical function that collects Window Value)
void TareaView::recogerValor(QString v, TareaView &tv){
    //funciones especiales correo equipo produccion.
    if(v==""){
        g_s->msgError("Nada seleccionado, se envia normal");
        tv.avisarAsignado();
    }
    if(v=="0"){
        g_s->msgError("Madera");

    }
    if(v=="1"){
        g_s->msgError("Metaclilato");

    }
    if(v=="2"){
        g_s->msgError("Ambos");

    }
}
    
asked by user3158502 30.08.2018 в 12:16
source

2 answers

2

In C ++ the functions that can be called without an object instance are known as static functions ... since " static " does not refer to movement but to its independence, functions that They can only be called with an object instance they are known as member functions (they are not called dynamic functions).

The member functions receive a " hidden parameter " which is the pointer this of the object to which they belong; they will use this pointer to access the internal data that the function may use when executed; for example (in your case) m_enviando_email_al_asignado .

I can not know what your program needs to correct itself due to the lack of context, but if you change your static function to receive an instance:

void TareaView::recogerValor(QString v, TareaView &tv){
//        Instancia a la que se avisa la asignación --> ~~~
    if(v==""){
        g_s->msgError("Nada seleccionado");
        tv.avisarAsignado(); // <--- Llamada a función miembro.
    }
    if(v=="0"){
        g_s->msgError("Madera");
    }
    if(v=="1"){
        g_s->msgError("Metaclilato");
    }
    if(v=="2"){
        g_s->msgError("Ambos");
    }
}

You will evade the error.

    
answered by 30.08.2018 / 12:41
source
0

Solved, indeed, was wrongly instantiating, finally, in the action of the accept button, I created the instance and then passed the value to the function "collectValue".

--------------------------------------------------------
TareaViewEquipo.cpp

void TareaViewEquipo::on_mui_aceptar_clicked()
{
    QString valor;
    TareaView *tv;                      <--------- ESTO
    valor=pasarvalor();
    TareaView::recogerValor(QString(valor),tv);  <-- Pasar bien la instancia
    close();
}

-----------------------------------------------------------------------------
TareaView.cpp

void TareaView::recogerValor(QString v, TareaView *tv){
    //funciones especiales correo equipo produccion.
    if(v==""){
        g_s->msgError("Nada seleccionado, se envia normal");
        tv->avisarAsignado();
    }

----------------------------------------------------------------

Thank you very much for your help:)

    
answered by 30.08.2018 в 17:32