error: no matching function for call to 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
因为它需要修改创建它的对象的成员变量,但是由于 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
}
希望这对以后的任何人都有帮助。
我试图在一个线程中 运行 一个名为 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
因为它需要修改创建它的对象的成员变量,但是由于 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
}
希望这对以后的任何人都有帮助。