In my class called Mat
, I want to have a function which takes another function as a parameter. Right now I have the 4 functions below, but I get an error in when calling print(). The second line gives me an error, but I don't understand why, since the first one works. The only difference is function f
is not a member of the class Mat
, but f2
is.
The failure is: error: no matching function for call to 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
",test(f ,5)); // works
printf("%d
",test(f2 ,5)); // does not work
}
Why does this happen?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…