错误 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; }
讯享网
- 修改后:
讯享网#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
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/83831.html