what does “error : a nonstatic member reference must be relative to a specific object” mean?

前端 未结 3 1116
不知归路
不知归路 2020-12-23 19:59
int CPMSifDlg::EncodeAndSend(char *firstName, char *lastName, char *roomNumber, char *userId, char *userFirstName, char *userLastName)
{
    ...

    return 1;
}

ex         


        
相关标签:
3条回答
  • 2020-12-23 20:22

    Only static functions are called with class name.

    classname::Staicfunction();
    

    Non static functions have to be called using objects.

    classname obj;
    obj.Somefunction();
    

    This is exactly what your error means. Since your function is non static you have to use a object reference to invoke it.

    0 讨论(0)
  • 2020-12-23 20:24

    CPMSifDlg::EncodeAndSend() method is declared as non-static and thus it must be called using an object of CPMSifDlg. e.g.

    CPMSifDlg obj;
    return obj.EncodeAndSend(firstName, lastName, roomNumber, userId, userFirstName, userLastName);
    

    If EncodeAndSend doesn't use/relate any specifics of an object (i.e. this) but general for the class CPMSifDlg then declare it as static:

    class CPMSifDlg {
    ...
      static int EncodeAndSend(...);
      ^^^^^^
    };
    
    0 讨论(0)
  • 2020-12-23 20:29

    EncodeAndSend is not a static function, which means it can be called on an instance of the class CPMSifDlg. You cannot write this:

     CPMSifDlg::EncodeAndSend(/*...*/);  //wrong - EncodeAndSend is not static
    

    It should rather be called as:

     CPMSifDlg dlg; //create instance, assuming it has default constructor!
     dlg.EncodeAndSend(/*...*/);   //correct 
    
    0 讨论(0)
提交回复
热议问题