c++ - <unresolved overloaded function type> -
in class called mat, want have function, takes function parameter. right got these 4 functions, error in when calling print(). second line gives me error, don't understand why, if first 1 works. difference function f not member of class mat, f2 is. says: error: no matching function call 'mat::test( < unresolved overloaded function type>, int)'"
template <typename f> int mat::test(f f, int v){ return f(v); } int mat::f2(int x){ return x*x; } int f(int x){ return x*x; } void mat::print(){ printf("%d\n",test(f ,5)); // works printf("%d\n",test(f2 ,5)); // not work }
why happen?
the type of pointer-to-member-function
different pointer-to-function
.
the type of function different depending on whether ordinary function or non-static member function of class:
int f(int x); type "int (*)(int)" // since ordinary function
and
int mat::f2(int x); type "int (mat::*)(int)" // since non-static member function of class mat
note: if it's static member function of class fred, type same if ordinary function: "int (*)(char,float)"
in c++, member functions have implicit parameter points object (the pointer inside member function). normal c functions can thought of having different calling convention member functions, so types of pointers (pointer-to-member-function vs pointer-to-function) different , incompatible. c++ introduces new type of pointer, called pointer-to-member, can invoked providing object.
note: not attempt "cast" pointer-to-member-function pointer-to-function; result undefined , disastrous. e.g., a pointer-to-member-function not required contain machine address of appropriate function. said in last example, if have pointer regular c function, use either top-level (non-member) function, or static (class) member function.
Comments
Post a Comment