Reputation: 361
#define UCHAR unsigned char
typedef bool (*FUNC)(UCHAR uc1, UCHAR uc2);
typedef void(*PF)(FUNC, UCHAR*);
PF Assign;
class Class {
private:
UCHAR buf[32];
bool func(UCHAR c1, UCHAR c2) { }
public:
Class::Class(void) {
Assign( func, buf ); // <<< Microsoft VC++ error C3867
}
Class::~Class() { }
};
error C3867: 'Class::func': function call missing argument list; use '&Class::func' to create a pointer to member
If I try the suggestion in the error message above
Assign( &Class::func, buf ); // <<< Microsoft VC++ error C2664
I get this error:
error C2664: 'void (FUNC,unsigned char *)' : cannot convert parameter 1 from 'bool (__thiscall Class::* )(unsigned char,unsigned char)' to 'FUNC' There is no context in which this conversion is possible
Without changing anything else how can I get this Assign() function to compile? These typedefs are from a library that I need to interface with.
Upvotes: 1
Views: 896
Reputation: 409166
The type-aliases FUNC
and PF
are not a member function pointers, and can only be used to point to static member functions. The reason being that all non-static member functions have an implicit first argument for the this
pointer.
If you need generic function "pointers" you should look into std::function
:
typedef std::function<bool(UCHAR uc1, UCHAR uc2)> FUNC;
typedef std::function<void(FUNC, UCHAR*)> PF;
PF Assign;
Class::Class(void) {
Assign( std::bind(&Class::func, *this), buf );
}
Upvotes: 1
Reputation: 171117
func
is a non-static member function, so its type is bool (Class::*)(UCHAR, UCHAR)
, while Assign
requires bool (*)(UCHAR, UCHAR)
. You cannot convert a non-static member function to a non-member function.
To be able to pass func
into Assign
, you'll have to make func
static.
Upvotes: 1