#include <functional>#include <iostream>#include <cmath>#include "numerical.hxx"int main(){ double start = 0.785398163; double step = 0.1; int order = 2; std::function<double(double)> f_sin = std::sin; std::cout << fdiff(start,step,order,f_sin) << std::endl; return 0;}
试图编译上面的程序给我错误(在clang中)
test.cpp:11:32: error: no viable conversion from '<overloaded function type>' to 'std::function<double (double)>' std::function<double(double)> f_sin = std::sin; ^ ~~~~~~~~/usr/lib/gcc/x86_64-unkNown-linux-gnu/4.7.1/../../../../include/c++/4.7.1/functional:2048:7: note: candIDate constructor not viable: no overload of 'sin' matching 'nullptr_t' for 1st argument function(nullptr_t) noexcept ^/usr/lib/gcc/x86_64-unkNown-linux-gnu/4.7.1/../../../../include/c++/4.7.1/functional:2059:7: note: candIDate constructor not viable: no overload of 'sin' matching 'const std::function<double (double)> &' for 1st argument function(const function& __x); ^/usr/lib/gcc/x86_64-unkNown-linux-gnu/4.7.1/../../../../include/c++/4.7.1/functional:2068:7: note: candIDate constructor not viable: no overload of 'sin' matching 'std::function<double (double)> &&' for 1st argument function(function&& __x) : _Function_base() ^/usr/lib/gcc/x86_64-unkNown-linux-gnu/4.7.1/../../../../include/c++/4.7.1/functional:2092:2: note: candIDate template ignored: Couldn't infer template argument '_Functor' function(_Functor __f,^1 error generated.
或者来自g
test.cpp: In function ‘int main()’:test.cpp:11:45: error: conversion from ‘<unresolved overloaded function type>’ to non-scalar type ‘std::function<double(double)>’ requested
据我所知,这是因为std :: sin是作为标准库中的模板类实现的,但我似乎无法弄清楚我需要做些什么来提供足够的特化来获取函数引用.我也尝试了各种各样的东西,比如使用new auto关键字,使用& std :: sin来获取指针等,但它们都给了我相同类型的错误.
解决方法std::sin
是一个重载函数:你必须消除你所指的std :: sin重载的歧义: std::function<double(double)> f_sin = (double(*)(double))&std::sin;
在某些情况下,编译器可以消除重载函数的歧义(例如,如果f_sin的类型为double(*)(double),则不需要强制转换).但是,这不是其中之一.
总结以上是内存溢出为你收集整理的c – 设置std :: function变量以引用std :: sin函数全部内容,希望文章能够帮你解决c – 设置std :: function变量以引用std :: sin函数所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)