错误 C2672 “std::invoke”: 未找到匹配的重载函数
多线程,参数不匹配,没有使用重载函数
- 报错,
#include <iostream>
#include <thread>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
const int d0 = 300;
void filter(Mat& H, int d0);
int main(int argc, char** argv) {
Mat src = imread("D://7.bmp", -1);
Mat H1(size(src), CV_64F);
Mat H2(size(src), CV_64F);
//filter(H1, 200); /// 单线程正确
//filter(H2, 400);
thread t1(filter, H1, 200);
thread t2(filter, H2, 400);
t1.join();
t2.join();
waitKey(0);
return 0;
}
报错:严重性 代码 说明 项目 文件 行 禁止显示状态
错误 C2672 “std::invoke”: 未找到匹配的重载函数 opencv11 C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.22.27905\include\thread 39
- 修改后:
#include <iostream>
#include <thread>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
const int d0 = 300;
void filter(Mat& H, int d0);
int main(int argc, char** argv) {
Mat src = imread("D://7.bmp", -1);
Mat H1(size(src), CV_64F);
Mat H2(size(src), CV_64F);
//filter(H1, 200); /// 单线程正确
//filter(H2, 400);
thread t1(filter, ref(H1), 200);
thread t2(filter, ref(H2), 400);
t1.join();
t2.join();
waitKey(0);
return 0;
}
filter是引用传递参数,但是thread构造的时候不知道filter的参数是引用的,thread只会盲目地复制H1的值,而这个复制出来的值是const的类型,这与filter需要的参数类型不匹配,因为filter需要的是non-const的引用,因此报错。
可以改成thread t(filter,ref(H1),200);
具体可以参考书籍C++ Concurrency in Action第2.2节Passing arguments to a thread function