May i know why this is not working ?
class aclass{
void (*FnPtr)(void) ;
void Function( void );
public:
.
.
.
.
.
}
if i write inside a body of a class function
FnPtr=Function;
the compiler gives me an error , why ? am i doing something wrong if i put the functio and the pointer outside the class
works....
tried everything doesn''t work this is the error i get
error C2440: ''='' : cannot convert from ''void (__thiscall _3DSFileParser::*)(void)'' to ''void (__cdecl *)(void)'' There is no context in which this conversion is possible
this happen only if the function and the pointer are inside the class , outside everything works fine how could that be ? some c++ guru out there ???
Inside class members, all member functions and variables implicitly have a "this->". It''s never necessary to explicitly use "this" to access data or members of a class from within that class.
The last suggestion won''t work unless he makes the data public; both the function and pointer are private in his implementation.
It looks like you''re doing the right thing with the function pointer. The error is probably elsewhere (like the other posters suggested, maybe a semi-colon missing).
Sorry, I was off on the private/public declaration when omitted. I already know about the ''this'', but if you you dealing with static functions in classes, you must use it to reference the instance at times, as the static function doesn''t know which instance called it.
But, the errors he is getting is consistent as C++ automatically inserts this as the first function argument, unless you specify the function as static:
class aClass { void (*FnPtr)(void); static void Function(void);
error C2440: '=' : cannot convert from 'void (__thiscall _3DSFileParser::*)(void)' to 'void (__cdecl *)(void)' There is no context in which this conversion is possible
The only difference is that one is called with __cdecl and one is __thiscall _3DSFileParser::*, you should be able to infer that your member variable is declared wrong, it should be:
void (3DSFileParser::*FnPtr)(void);
or, in the faked example
void (aclass::*FnPtr)(void);
Edited by - Don Neufeld on March 8, 2001 7:09:06 PM
This is because, FnPtr is a pointer of a global function. In C++, pointer to a function is different from pointer to a member function because a member function implicitely has a "this" pointer associated with it where as a global function does not have one.
Possible solutions for the above problem are:
Solution 1. Make the function "void Function (void)" as a static function or a global function. i.e
class aclass{
void (*FnPtr)(void) ;
static void Function( void );
public: . . . . . }
FnPtr=Function; // legal
Solution 2:
Declare FnPtr as a pointer to a member function instead of just pointer to a funtion.