不能在没有对象的情况下调用成员函数 = C++
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3304369/
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
Cannot call member function without object = C++
提问by jDOG
I am brushing up again and I am getting an error:
我再次刷牙,但出现错误:
Cannot call member function without object.
没有对象就不能调用成员函数。
I am calling like:
我打电话给:
FxString text = table.GetEntry(obj->GetAlertTextID());
FxUChar outDescription1[ kCP_DEFAULT_STRING_LENGTH ];
IC_Utility::CP_StringToPString(text, &outDescription1[0] );
The line: IC_Utility::CP_StringToPString(text, &outDescription1[0] ); is getting the error
该行: IC_Utility::CP_StringToPString(text, &outDescription1[0] ); 正在收到错误
My function is:
我的功能是:
void IC_Utility::CP_StringToPString( FxString& inString, FxUChar *outString)
{
}
I know it has to be something simple I am missing.
我知道它必须是我遗漏的一些简单的东西。
回答by Tim Robinson
If you've written the CP_StringToPString
function, you need to declare it static
:
如果您已经编写了该CP_StringToPString
函数,则需要声明它static
:
static void IC_Utility::CP_StringToPString( FxString& inString, FxUChar *outString)
Alternatively, if it's a function in third-party code, you need to declare an IC_Utility
object to call it on:
或者,如果它是第三方代码中的函数,则需要声明一个IC_Utility
对象来调用它:
IC_Utility u;
u.CP_StringToPString(text, &outDescription1[0] );
回答by Blair Conrad
Your method isn't static, and so it must be called from an instance (sort of like the error is saying). If your method doesn't require access to any other instance variables or methods, you probably just want to declare it static
. Otherwise, you'll have to obtain the correct instance and execute the method on that instance.
您的方法不是静态的,因此必须从实例调用它(有点像错误所说的)。如果您的方法不需要访问任何其他实例变量或方法,您可能只想声明它static
。否则,您必须获取正确的实例并在该实例上执行该方法。
回答by Gianni
You have to declare the function with the 'static' keyword:
您必须使用 'static' 关键字声明该函数:
class IC_Utility {
static void CP_StringToPString( FxString& inString, FxUChar *outString);
回答by Kristopher Johnson
You need to declare the function static
in your class declaration. e.g.
您需要static
在类声明中声明该函数。例如
class IC_Utility {
// ...
static void CP_StringToPString(FxString& inString, FxUChar *outString);
// ...
};
回答by ruslik
"static" is the right answer. or, you can pass it a NULL "this" pointer if it's not used in the function:
“静态”是正确的答案。或者,如果函数中未使用它,您可以向它传递一个 NULL“this”指针:
((IC_Utility*)NULL)->CP_StringToPString(...);