所以我想创建一个模板函数,它接受一个函数作为T类型的参数。
#include<functional>
template<typename T>
T bisection(T xL, T xR, T epsilon, std::function<T(T)> fx)现在,在主程序中,下面的调用给出了错误。
bisection(0.0, 2.0, 0.001, [](double x){return x*x-2;})错误消息:
bisection.cpp: In function ‘int main()’:
bisection.cpp:24:65: error: no matching function for call to
‘bisection(double, double, double, main()::<lambda(double)>)’
cout << bisection(0.0, 2.0, 0.001, [](double x){return x*x-2;}) << endl;
^
bisection.cpp:6:3: note: candidate: template<class T> T bisection(T, T,
T, std::function<T(T)>)
T bisection(T xL, T xR, T epsilon, std::function<T(T)> fx)
^
bisection.cpp:6:3: note: template argument deduction/substitution failed:
bisection.cpp:24:65: note: ‘main()::<lambda(double)>’ is not derived from ‘std::function<T(T)>’
cout << bisection(0.0, 2.0, 0.001, [](double x){return x*x-2;}) << endl;请建议如何纠正此错误。如果我将二等分的函数签名改为:
T bisection(T xL, T xR, T epsilon, std::function<double(double)> fx)发布于 2016-12-09 03:23:05
lambda不是std::function,更简单的应该是
template<typename T, template F>
T bisection(T xL, T xR, T epsilon, F&& fx);https://stackoverflow.com/questions/41047215
复制相似问题