44
int CPMSifDlg::EncodeAndSend(char *firstName, char *lastName, char *roomNumber, char *userId, char *userFirstName, char *userLastName)
{
    ...

    return 1;
}

extern "C"
{
    __declspec(dllexport) int start(char *firstName, char *lastName, char *roomNumber, char *userId, char *userFirstName, char *userLastName)
    {
        return CPMSifDlg::EncodeAndSend(firstName, lastName, roomNumber, userId, userFirstName, userLastName);
    }
}

On line return CPMSifDlg::EncodeAndSend I have an error : Error : a nonstatic member reference must be relative to a specific object.

What does it mean?

user8472
  • 3,001
  • 3
  • 30
  • 58
Oscar Yuandinata
  • 443
  • 1
  • 4
  • 4

3 Answers3

60

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 
Nawaz
  • 327,095
  • 105
  • 629
  • 812
  • but if i remove the "CPMSifDlg", i got "error : identifier EncodeAndSend is undefined" – Oscar Yuandinata Mar 22 '12 at 08:17
  • like this right? `CPMSifDlg dlg; return dlg.EncodeAndSend(firstName, lastName, roomNumber, userId, userFirstName, userLastName);` it says the method is inaccessible – Oscar Yuandinata Mar 22 '12 at 08:19
  • @OscarYuandinata: Yes. You need to create an instance of the class, in order to call the function. Or, make the function a static member function, or simply a free function. – Nawaz Mar 22 '12 at 08:20
  • like this right? `CPMSifDlg dlg; return dlg.EncodeAndSend(firstName, lastName, roomNumber, userId, userFirstName, userLastName);` it says the method is inaccessible – Oscar Yuandinata Mar 22 '12 at 08:23
  • the question is, if they are friend classes, it will still have this error, which does not make any sense. strange – Dexter Sep 06 '20 at 13:50
9

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(...);
  ^^^^^^
};
iammilind
  • 62,239
  • 27
  • 150
  • 297
9

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.

Rohit Vipin Mathews
  • 10,853
  • 15
  • 49
  • 100