小编Mor*_*rag的帖子

c ++ - <未解析的重载函数类型>

在我调用的类中Mat,我希望有一个函数,它将另一个函数作为参数.现在我有以下4个函数,但是在调用print()时遇到错误.第二行给了我一个错误,但我不明白为什么,因为第一行有效.唯一的区别是函数f不是类的成员Mat,而是f2.失败的是: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\n",test(f ,5));    // works
    printf("%d\n",test(f2 ,5));    // does not work
}
Run Code Online (Sandbox Code Playgroud)

为什么会这样?

c++ templates function

29
推荐指数
2
解决办法
5万
查看次数

标签 统计

c++ ×1

function ×1

templates ×1