簡體   English   中英

錯誤:沒有用於調用 std::thread 的匹配函數

[英]error: no matching function for call to std::thread

我試圖在一個線程中運行一個名為dcp的函數,我必須獨立運行這個函數三次。 所以這就是我的實現方式:

void dcp(cv::Mat&, int, int, cv::Mat&, double);

int main(int argc, char* argv[])
{
cv::Mat IllumTrans;
//fill IllumTrans

std::vector<cv::Mat> rgbDCP;
rgbDCP.reserve(3);
//Fill it

std::thread thread_1(dcp, rgb[0], rows, cols, IllumTrans, A[0]);
std::thread thread_2(dcp, rgb[1], rows, cols, IllumTrans, A[1]);
std::thread thread_3(dcp, rgb[2], rows, cols, IllumTrans, A[2]);

thread_1.join();
thread_2.join();
thread_3.join();
}

但是我得到了沒有匹配函數的錯誤調用:

In file included from 21022018WorksfineOneimageThread.cpp:6:0:
/usr/include/c++/7/thread: In instantiation of ‘struct std::thread::_Invoker<std::tuple<void (*)(cv::Mat&, int, int, cv::Mat&, double), cv::Mat, int, int, cv::Mat, int> >’:
/usr/include/c++/7/thread:127:22:   required from ‘std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (&)(cv::Mat&, int, int, cv::Mat&, double); _Args = {cv::Mat&, int&, int&, cv::Mat&, int&}]’
21022018WorksfineOneimageThread.cpp:136:65:   required from here
/usr/include/c++/7/thread:240:2: error: no matching function for call to ‘std::thread::_Invoker<std::tuple<void (*)(cv::Mat&, int, int, cv::Mat&, double), cv::Mat, int, int, cv::Mat, int> >::_M_invoke(std::thread::_Invoker<std::tuple<void (*)(cv::Mat&, int, int, cv::Mat&, double), cv::Mat, int, int, cv::Mat, int> >::_Indices)’
  operator()()
  ^~~~~~~~
/usr/include/c++/7/thread:231:4: note: candidate: template<long unsigned int ..._Ind> decltype (std::__invoke((_S_declval<_Ind>)()...)) std::thread::_Invoker<_Tuple>::_M_invoke(std::_Index_tuple<_Ind ...>) [with long unsigned int ..._Ind = {_Ind ...}; _Tuple = std::tuple<void (*)(cv::Mat&, int, int, cv::Mat&, double), cv::Mat, int, int, cv::Mat, int>]
    _M_invoke(_Index_tuple<_Ind...>)
    ^~~~~~~~~
/usr/include/c++/7/thread:231:4: note:   template argument deduction/substitution failed:
/usr/include/c++/7/thread: In substitution of ‘template<long unsigned int ..._Ind> decltype (std::__invoke(_S_declval<_Ind>()...)) std::thread::_Invoker<std::tuple<void (*)(cv::Mat&, int, int, cv::Mat&, double), cv::Mat, int, int, cv::Mat, int> >::_M_invoke<_Ind ...>(std::_Index_tuple<_Ind1 ...>) [with long unsigned int ..._Ind = {0, 1, 2, 3, 4, 5}]’:
/usr/include/c++/7/thread:240:2:   required from ‘struct std::thread::_Invoker<std::tuple<void (*)(cv::Mat&, int, int, cv::Mat&, double), cv::Mat, int, int, cv::Mat, int> >’
/usr/include/c++/7/thread:127:22:   required from ‘std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (&)(cv::Mat&, int, int, cv::Mat&, double); _Args = {cv::Mat&, int&, int&, cv::Mat&, int&}]’
21022018WorksfineOneimageThread.cpp:136:65:   required from here
/usr/include/c++/7/thread:233:29: error: no matching function for call to ‘__invoke(std::__tuple_element_t<0, std::tuple<void (*)(cv::Mat&, int, int, cv::Mat&, double), cv::Mat, int, int, cv::Mat, int> >, std::__tuple_element_t<1, std::tuple<void (*)(cv::Mat&, int, int, cv::Mat&, double), cv::Mat, int, int, cv::Mat, int> >, std::__tuple_element_t<2, std::tuple<void (*)(cv::Mat&, int, int, cv::Mat&, double), cv::Mat, int, int, cv::Mat, int> >, std::__tuple_element_t<3, std::tuple<void (*)(cv::Mat&, int, int, cv::Mat&, double), cv::Mat, int, int, cv::Mat, int> >, std::__tuple_element_t<4, std::tuple<void (*)(cv::Mat&, int, int, cv::Mat&, double), cv::Mat, int, int, cv::Mat, int> >, std::__tuple_element_t<5, std::tuple<void (*)(cv::Mat&, int, int, cv::Mat&, double), cv::Mat, int, int, cv::Mat, int> >)’
    -> decltype(std::__invoke(_S_declval<_Ind>()...))
                ~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/7/tuple:41:0,
                 from /usr/include/c++/7/functional:54,
                 from /usr/local/include/opencv2/core/utility.hpp:60,
                 from /usr/local/include/opencv2/core.hpp:3261,
                 from 21022018WorksfineOneimageThread.cpp:1:
/usr/include/c++/7/bits/invoke.h:89:5: note: candidate: template<class _Callable, class ... _Args> constexpr typename std::__invoke_result<_Functor, _ArgTypes>::type std::__invoke(_Callable&&, _Args&& ...)
     __invoke(_Callable&& __fn, _Args&&... __args)
     ^~~~~~~~
/usr/include/c++/7/bits/invoke.h:89:5: note:   template argument deduction/substitution failed:
/usr/include/c++/7/bits/invoke.h: In substitution of ‘template<class _Callable, class ... _Args> constexpr typename std::__invoke_result<_Functor, _ArgTypes>::type std::__invoke(_Callable&&, _Args&& ...) [with _Callable = void (*)(cv::Mat&, int, int, cv::Mat&, double); _Args = {cv::Mat, int, int, cv::Mat, int}]’:
/usr/include/c++/7/thread:233:29:   required by substitution of ‘template<long unsigned int ..._Ind> decltype (std::__invoke(_S_declval<_Ind>()...)) std::thread::_Invoker<std::tuple<void (*)(cv::Mat&, int, int, cv::Mat&, double), cv::Mat, int, int, cv::Mat, int> >::_M_invoke<_Ind ...>(std::_Index_tuple<_Ind1 ...>) [with long unsigned int ..._Ind = {0, 1, 2, 3, 4, 5}]’
/usr/include/c++/7/thread:240:2:   required from ‘struct std::thread::_Invoker<std::tuple<void (*)(cv::Mat&, int, int, cv::Mat&, double), cv::Mat, int, int, cv::Mat, int> >’
/usr/include/c++/7/thread:127:22:   required from ‘std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (&)(cv::Mat&, int, int, cv::Mat&, double); _Args = {cv::Mat&, int&, int&, cv::Mat&, int&}]’
21022018WorksfineOneimageThread.cpp:136:65:   required from here
/usr/include/c++/7/bits/invoke.h:89:5: error: no type named ‘type’ in ‘struct std::__invoke_result<void (*)(cv::Mat&, int, int, cv::Mat&, double), cv::Mat, int, int, cv::Mat, int>’

我正在像這樣編譯我的程序:

g++ -std=c++1z -fomit-frame-pointer -O3 -ffast-math -mmmx -msse -msse2 -msse3 -DNDEBUG -Wall 21022018WorksfineOneimageThread.cpp -o 21022018WorksfineOneimageThread -fopenmp `pkg-config --cflags --libs opencv`

gcc version 7.3.0 (Ubuntu 7.3.0-16ubuntu3~16.04.1) 

您的函數需要一個引用( cv::Mat& )作為它的第一個和第四個參數,但您只傳遞cv::Mat ,因此您應該將它們包裝在std::ref

std::thread thread_1(dcp, std::ref(rgb[0]), rows, cols, std::ref(IllumTrans), A[0]);

我想指出一些事情,以防有人遇到和我一樣的問題。

我在從const成員函數中啟動線程時遇到了類似的問題。 線程要執行的const不是const因為它需要修改創建它的對象的成員變量,但是由於 this 指針是const由於從const函數調用,編譯器抱怨類似的錯誤到 OP。

我不能分享我的代碼,但我會分享一些類似的東西來演示這個問題。

void FooClass::setup() const  // The const here was my problem
{
  // Here, the 'this' pointer is const due to the setup function being const
  // but the thread_routine function expects it no to be const.
  std::thread(&FooClass::thread_routine, this).detach();
}

void FooClass::thread_routine()
{
  // Do something with member variables
}

希望這對未來的任何人都有幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM