I'm trying to do something basic in C++, create a pointer to a function but it's not working.
In the header file I have this:
void (*ptr)(unsigned char*, int, int);
void func01(unsigned char*, int, int);
And in the implementation file I have this:
ptr=&func01;
OR
C::ptr=&C::func01;
etc. Still get errors
And this is the error I get:
error C2440: '=' : cannot convert from 'void (__thiscall C::* )(unsigned char *,int,int)' to 'void (__cdecl *)(unsigned char *,int,int)'
1> There is no context in which this conversion is possible
error C2276: '&' : illegal operation on bound member function expression
If I use:
ptr=func01;
I get:
error C3867: 'C::func01': function call missing argument list; use '&C::func01' to create a pointer to member
error C2440: '=' : cannot convert from 'void (__thiscall C::* )(unsigned char *,int,int)' to 'void (__cdecl *)(unsigned char *,int,int)'
1> There is no context in which this conversion is possible
SO, if I go
ptr=&C::func01;
I get this:
error C2440: '=' : cannot convert from 'void (__thiscall C::* )(unsigned char *,int,int)' to 'void (__cdecl *)(unsigned char *,int,int)'
1> There is no context in which this conversion is possible
OR
ptr=&func01;
I get:
error C2276: '&' : illegal operation on bound member function expression
Somewhere I'm making some kind of trivial yet fundamental mistake but I can't figure it out and any help is appreciated.
#include <iostream>
struct F
{
void g(int a)
{
std::cout << a;
}
void f()
{
fptr = &F::g;
}
void (F::* fptr)(int);
};
int main()
{
F a;
a.f();
(a.*a.fptr)(23);
return 0;
}
I believe it's impossible to call the function without an object, so you'll probably only be able to use these function pointers inside the class itself.