C++ “错误:非静态成员引用必须相对于特定对象”是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9818515/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
what does "error : a nonstatic member reference must be relative to a specific object" mean?
提问by Oscar Yuandinata
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.
在线return CPMSifDlg::EncodeAndSend
我有一个错误:错误:非静态成员引用必须相对于特定对象。
What does it mean?
这是什么意思?
回答by Nawaz
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
回答by Rohit Vipin Mathews
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.
这正是您的错误的含义。由于您的函数是非静态的,因此您必须使用对象引用来调用它。
回答by iammilind
CPMSifDlg::EncodeAndSend()
method is declared as non-static
and thus it must be called using an object of CPMSifDlg
. e.g.
CPMSifDlg::EncodeAndSend()
方法被声明为非static
,因此必须使用 的对象调用它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
)但一般用于class CPMSifDlg
然后将其声明为static
:
class CPMSifDlg {
...
static int EncodeAndSend(...);
^^^^^^
};