views:

170

answers:

5

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.

A: 

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

class IC_Utility {
   // ...

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

   // ...
};
Kristopher Johnson
@kristopher - declare static since I dont create an object first like IC_Utility u; u.xxx(); etc?Do you have a link to something I could read?
jDOG
Here's something to read: http://www.ehow.com/how_2189209_declare-static-functions-c.htmlBut, it's really a pretty basic feature of C++. You may want to just pick up a good C++ book/tutorial.
Kristopher Johnson
+6  A: 

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.

Blair Conrad
+2  A: 

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] );
Tim Robinson
A: 

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

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

"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(...);
ruslik
[Leads to undefined behavior](http://stackoverflow.com/questions/2474018/when-does-invoking-a-member-function-on-a-null-instance-result-in-undefined-behav), don't do it.
GMan
@GMman if it could be made static , then there won't be ANY undefined behavior when it's called this way. Show me at least one disassembly listing that proves me wrong.
ruslik
@ruslik: What does disassembly have to do with anything? In the *language* C++, this leads to undefined behavior. Certain output on certain implementations under certain conditions has nothing to do with C++.
GMan
@GMan: AFAIK, in C++ the member functions use __thiscall calling convention, and receives an additional parameter, the "this" pointer, that they can use. the static member functions use __cdecl calling convention, and are exactly like C functions. Both types of functions are declared in the namespace of the the class. For non-virtual functions this namespace is deduced at compile time and static linking is used in both cases. Now, a function could be declared static if it doesn't require anything from "this". So, where's the undefined behaviour?
ruslik
@ruslik: `__thiscall`,` __cdecl`, etc... are all implementation details. Again: Certain output on certain implementations under certain conditions has nothing to do with C++. The moment you assume some implementation detail we aren't talking about C++, we are talking about some compiler on some platform. The language is not defined by how your compiler implements it, it's defined by a standard. The standard says it leads to undefined behavior, so it does. (Put another way: The standard doesn't guarantee `__thiscall` and friends even exist, so how can you argue defined behavior with those?)
GMan