Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用std::function和bind分配具有不同参数列表的函数_C++_Opencv_C++11_Std Function_Stdbind - Fatal编程技术网

C++ 使用std::function和bind分配具有不同参数列表的函数

C++ 使用std::function和bind分配具有不同参数列表的函数,c++,opencv,c++11,std-function,stdbind,C++,Opencv,C++11,Std Function,Stdbind,我尝试使用一个函数指针,在某些情况下,它要么被分配一个函数,该函数包含两个参数(一个cv::Mat和一个包含参数的结构),要么被分配一个不同的函数,该函数包含三个参数(相同的两个参数和一个坐标列表)。我认为std::function和std::bind是我应该在这里使用的 Mat process_F1(cv::Mat img, feature_params f); Mat process_F1_coords(cv::Mat img, feature_params f, std::vector&l

我尝试使用一个函数指针,在某些情况下,它要么被分配一个函数,该函数包含两个参数(一个cv::Mat和一个包含参数的结构),要么被分配一个不同的函数,该函数包含三个参数(相同的两个参数和一个坐标列表)。我认为std::function和std::bind是我应该在这里使用的

Mat process_F1(cv::Mat img, feature_params f);
Mat process_F1_coords(cv::Mat img, feature_params f, std::vector<std::pair<int, int> > feature_coords c);
Mat process_F2(cv::Mat img, feature_params f);
Mat process_F2_coords(cv::Mat img, feature_params f, std::vector<std::pair<int, int> > feature_coords );
// this is the function pointer that will hold them
std::function<cv::Mat()> feature_func_f;
//this is how I assign them:
void set_feature_func(int feature_method, bool use_coords)
{
    switch (feature_method){
    case 0:
            if( !use_coords )
                feature_func_f = std::bind(process_F1,std::placeholders::_2);
            else
                feature_func_f = std::bind(process_F1_coords,std::placeholders::_3);  
            break;
        case 1:
            if( !use_coords )
                feature_func_f = std::bind(process_F2,std::placeholders::_2);
            else
                feature_func_f = std::bind(process_F2_coords,std::placeholders::_3);   
            break;
}
Mat过程F1(cv::Mat img,特征参数f);
Mat过程协调(cv::Mat img,特征参数f,std::向量特征协调c);
Mat过程F2(cv::Mat img,特征参数f);
Mat过程坐标(cv::Mat img、特征参数f、std::向量特征坐标);
//这是保存它们的函数指针
std::函数特征函数;
//我是这样分配它们的:
void set\u feature\u func(int feature\u方法,bool use\u坐标)
{
开关(特性法){
案例0:
如果(!使用坐标)
feature_func_f=std::bind(进程F1,std::占位符2);
其他的
feature_func_f=std::bind(进程协调,std::占位符::3);
打破
案例1:
如果(!使用坐标)
feature_func_f=std::bind(进程,std::占位符::2);
其他的
feature_func_f=std::bind(进程协调,std::占位符::3);
打破
}
我打算将功能函数称为:

cv::Mat m, n;
feature_params p;
set_feature_func(0,false);

n = feature_func_f(m,p);

// or if I have a coordinate list c
std::vector<std::pair<int, int> > c;

set_feature_func(0,true);
n = feature_func_f(m,p,c);
cv::Mat m,n;
特征参数p;
set_feature_func(0,false);
n=特征函数f(m,p);
//或者如果我有一个坐标列表c
std::向量c;
设置功能函数(0,真);
n=特征函数f(m,p,c);
我在这里做错了什么?我在函数的标题中得到了一堆没有真正意义的错误:

Error   4   error C2977: 'std::add_reference' : too many template arguments C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\functional   900 1   
Error   5   error C2955: 'std::add_reference' : use of class template requires template argument list   C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\functional   900 1   
Error   6   error C2198: 'cv::Mat (__cdecl *)(cv::Mat,feature_params)' : too few arguments for call C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\functional   1149    1   
Error   2   error C2146: syntax error : missing ',' before identifier 'type'    C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\functional   900 1   
Error   3   error C2065: 'type' : undeclared identifier C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\functional   900 1   
Error   1   error C2027: use of undefined type 'std::tuple_element<0x01,_Ftuple>'   C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\functional   900 1   
Error 4 Error C2977:'std::add_reference':模板参数太多C:\Program Files(x86)\Microsoft Visual Studio 12.0\VC\include\functional 900 1
错误5错误C2955:'std::add_reference':使用类模板需要模板参数列表C:\Program Files(x86)\Microsoft Visual Studio 12.0\VC\include\functional 900 1
错误6错误C2198:'cv::Mat(_cdecl*)(cv::Mat,功能参数)':调用C:\Program Files(x86)\Microsoft Visual Studio 12.0\VC\include\functional 1149 1的参数太少
错误2错误C2146:语法错误:缺少“,”在标识符之前“类型”C:\Program Files(x86)\Microsoft Visual Studio 12.0\VC\include\functional 900 1
错误3错误C2065:“类型”:未声明的标识符C:\Program Files(x86)\Microsoft Visual Studio 12.0\VC\include\functional 900 1
错误1错误C2027:使用未定义的类型“std::tuple\u element”C:\Program Files(x86)\Microsoft Visual Studio 12.0\VC\include\functional 900 1

不,您不能随心所欲。
std::bind
仅用于绑定参数,不应在函数调用中使用。您可以使用两个函数,一个用于2个参数,一个用于3个参数,或者向函数
set\u feature\u func
发送坐标

这只是一个关于使用
std::function
std::bind
的简单示例

#include <functional>
#include <iostream>

int main()
{
   std::function<void(int)> function;
   auto l1 = [](int v) { std::cout << v << std::endl; };
   auto l2 = [](int v1, int v2) { std::cout << v1 << " " << v2 << std::endl; };
   function = l1;
   function(5);
   function = std::bind(l2, std::placeholders::_1, 10);
   function(5);
}
#包括
#包括
int main()
{
std::函数;

auto l1=[](int v){std::cout一般来说,变量的类型决定了可以对变量执行何种操作

feature\u func\u f
只有一种类型。以前的函数调用无法更改其类型

std::function
支持一个参数签名

因此,
n=feature\u func\u f(m,p);
n=feature\u func\u f(m,p,c);
不能同时在
std::function
上有效

有很多方法可以解决这个问题,但在这种情况下没有什么意义。使用两个
功能函数,每个都有不同的签名,然后调用正确的一个

std::function<cv::Mat(cv::Mat img, feature_params f)> feature_func_f;
std::function<cv::Mat(cv::Mat img, feature_params f, std::vector<std::pair<int,int>>)> feature_func_vector_f;
std::函数特性函数;
函数特征函数向量;

删除不影响您的问题的内容,如
匹配器::
并让它自己编译并生成错误。请发布每个错误,或至少发布第一组错误(从顶部开始的错误,然后转到提到代码的部分)--随机选择的错误没有那么有用。等一下,你认为
\u 2
\u 3
是占位符的数量吗?你是否成功地使用过
std::placeholder
?你打算如何调用
功能函数?
?我澄清了问题,并澄清了如何调用该函数。
std::vector<std::pair<int, int> > c;

set_feature_func(0,true,c);
n = feature_func_f(m,p);
std::function<cv::Mat(cv::Mat img, feature_params f)> feature_func_f;
std::function<cv::Mat(cv::Mat img, feature_params f, std::vector<std::pair<int,int>>)> feature_func_vector_f;