Cannot call member function without object = C++

会有一股神秘感。 提交于 2019-12-21 03:49:20

问题


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

My function is:

void IC_Utility::CP_StringToPString( FxString& inString, FxUChar *outString)
{
}

I know it has to be something simple I am missing.


回答1:


If you've written the CP_StringToPString function, you need to declare it 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 u;
u.CP_StringToPString(text, &outDescription1[0] );



回答2:


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.




回答3:


You have to declare the function with the 'static' keyword:

class IC_Utility {
    static void CP_StringToPString( FxString& inString, FxUChar *outString);



回答4:


You need to declare the function static in your class declaration. e.g.

class IC_Utility {
   // ...

   static void CP_StringToPString(FxString& inString, FxUChar *outString);

   // ...
};



回答5:


"static" is the right answer. or, you can pass it a NULL "this" pointer if it's not used in the function:

((IC_Utility*)NULL)->CP_StringToPString(...);


来源:https://stackoverflow.com/questions/3304369/cannot-call-member-function-without-object-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!