I have a piece of code like this
class A {
public:
typedef int (A::*AFn)(int);
std::map<std::string, AFn> fm_;
A() {
fm_.insert("fn1", fn);
}
int fn(int a) {
return a;
}
};
I get a compile time error saying error: reference to non-static member function must be called fm_.insert("fn1", fn);
Why does this happen and how do I correct it?
Since fn
is a non-static member function, a lone fn
is not a valid expression. The only thing you can do with non-qualified fn
in this context is call it: fn(something)
. This is what the compiler is telling you.
If you want to obtain a pointer to member function A::fn
, you have to explcitly use operator &
and supply a qualified member name: &A::fn
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.