2
int Foo(int x) {
}

int Thing::Foo(int x) {
}

I'm trying to call a function that expects a void* InCallback, when I call it as Function(Foo) it does work.

When I try to call it Function(Thing::Foo) i get this error:

argument of type "int (Thing::*)(int x)" is incompatible with parameter of type "void *"

Is possible somehow to call the class function?

  • And what the value of `this` should be in that call? How would you pass that value even theoretically? If `this` is not actually used in `Thing::Foo()`, just make it `static`. – Evg Sep 17 '21 at 13:25
  • @Evg sorry, i don't understand your question. –  Sep 17 '21 at 13:28
  • 1
    To call `Thing::Foo()`, you need **two** values: `int` argument and `this` pointer to an object. When a callback `void(int)` is called via a function pointer, only `int` is passed to it. What about `this`? How could it be passed? – Evg Sep 17 '21 at 13:30
  • `void* InCallback` is not a function pointer. The code is already suspect, since function pointers and data pointers are not interchangeable. Your Thing::Foo is probably not marked `static` in the declaration. If it were, the code should work (ignoring the data pointer and function pointer conflation). – Eljay Sep 17 '21 at 13:33
  • You mean that `Function` is declared as ` Function(void *)`? – JaMiT Sep 17 '21 at 13:41
  • @JaMiT yes, i also tried to cast it to void with `Function( void* (Thing::Foo) )` –  Sep 17 '21 at 13:42
  • @Eljay when i mark it as static i get this warning on compilation `LNK2001 unresolved external symbol "public: static void __cdecl Thing::Foo(void)" (?Foo@Thing@@SAXXZ) ` –  Sep 17 '21 at 13:44
  • The situation is basically the same as in [Convert pointer to data member to void *](https://stackoverflow.com/questions/15547305/), but I'm hesitant to say a question about a pointer-to-member-function is the same as a question about a pointer-to-data-member. – JaMiT Sep 17 '21 at 13:49
  • Unresolved external is not about compilation, it is about linking. Please show a complete code example that produces this error. – Evg Sep 17 '21 at 14:01

0 Answers0