Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot call member function without object = C++

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.

like image 564
jDOG Avatar asked Jul 21 '10 22:07

jDOG


People also ask

Can we call member functions without an object?

You cannot have static and nonstatic member functions with the same names and the same number and type of arguments. Like static data members, you may access a static member function f() of a class A without using an object of class A .

How do you call a class member function without an object?

Use Static Member Functions Static member functions are the functions of a class that do not need an object to call them. They can be called directly with the class name using the scope resolution operator :: .

How do you call a pointer to a member function in C++?

The pointer to member operators . * and ->* are used to bind a pointer to a member of a specific class object. Because the precedence of () (function call operator) is higher than . * and ->* , you must use parentheses to call the function pointed to by ptf .


2 Answers

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] );
like image 197
Tim Robinson Avatar answered Sep 20 '22 00:09

Tim Robinson


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.

like image 27
Blair Conrad Avatar answered Sep 20 '22 00:09

Blair Conrad