Function pointer problem


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.

RON
Last edited on
Could you please post the definition for func01()?
void func01(unsigned char*, int, int);

ONE THING I FORGOT TO MENTION:

func01 is a member function (in class C).


And the function pointer is also a member of class C.


RON
Please use code tags. I believe the correct syntax is for member functions is:

 
return_type (ClassName::* pointer_name)(input...);


And thus becomes:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#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.
Thanks and sorry: I didn't see it in your original post -- my bad.

C.ptr = C.func01 or C->ptr = C->func01, should work, no?
Sorry about the code tags. THANKS!!!!
Topic archived. No new replies allowed.