Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++ - <unresolved overloaded function type>

The type of pointer-to-member-function is different from pointer-to-function.

The type of a function is different depending on whether it is an ordinary function or a non-static member function of some class:

int f(int x);
the type is "int (*)(int)" // since it is an ordinary function

And

int Mat::f2(int x);
the type is "int (Mat::*)(int)" // since it is a non-static member function of class Mat

Note: if it's a static member function of class Fred, its type is the same as if it were an ordinary function: "int (*)(char,float)"

In C++, member functions have an implicit parameter which points to the object (the this pointer inside the member function). Normal C functions can be thought of as having a different calling convention from member functions, so the types of their pointers (pointer-to-member-function vs pointer-to-function) are different and incompatible. C++ introduces a new type of pointer, called a pointer-to-member, which can be invoked only by providing an object.

NOTE: do not attempt to "cast" a pointer-to-member-function into a pointer-to-function; the result is undefined and probably disastrous. E.g., a pointer-to-member-function is not required to contain the machine address of the appropriate function. As was said in the last example, if you have a pointer to a regular C function, use either a top-level (non-member) function, or a static (class) member function.

More on this Here and here.


The problem here is that f2 is a method on Mat, while f is just a free function. You can't call f2 by itself, it needs an instance of Mat to call it on. The easiest way around this might be:

printf("%d\n", test([=](int v){return this->f2(v);}, 5));

The = there will capture this, which is what you need to call f2.