“错误:非静态成员引用必须与特定对象相关”是什么意思?

时间:2022-05-15 09:45:24
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.

在线返回CPMSifDlg :: EncodeAndSend我有一个错误:错误:非静态成员引用必须相对于特定对象。

What does it mean?

这是什么意思?

3 个解决方案

#1


30  

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

EncodeAndSend不是静态函数,这意味着它可以在类CPMSifDlg的实例上调用。你不能写这个:

 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 

#2


7  

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

CPMSifDlg :: EncodeAndSend()方法声明为非静态,因此必须使用CPMSifDlg的对象进行调用。例如

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:

如果EncodeAndSend不使用/关联对象的任何细节(即this),但对CPMSifDlg类通用则声明为静态:

class CPMSifDlg {
...
  static int EncodeAndSend(...);
  ^^^^^^
};

#3


6  

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.

这正是您的错误所意味着的。由于您的函数是非静态的,因此您必须使用对象引用来调用它。

#1


30  

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

EncodeAndSend不是静态函数,这意味着它可以在类CPMSifDlg的实例上调用。你不能写这个:

 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 

#2


7  

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

CPMSifDlg :: EncodeAndSend()方法声明为非静态,因此必须使用CPMSifDlg的对象进行调用。例如

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:

如果EncodeAndSend不使用/关联对象的任何细节(即this),但对CPMSifDlg类通用则声明为静态:

class CPMSifDlg {
...
  static int EncodeAndSend(...);
  ^^^^^^
};

#3


6  

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.

这正是您的错误所意味着的。由于您的函数是非静态的,因此您必须使用对象引用来调用它。