如果我在线程内调用模板类的成员函数,则会收到此编译器错误。我已经在寻找类似的问题,但没有一个有帮助。如果有人能澄清这里的问题,我会感到很高兴,因为我认为这是概念性的,而不是特定于代码的。
在尝试编译较大的应用程序时出现此编译错误,并且我尽可能地简化了此错误以隔离错误源。 不确定是否相关,但是我正在使用std = c ++ 11
进行编译这是简化的代码:
#include <functional>
#include <vector>
#include <thread>
using namespace std;
template<class T> class my_class{
vector<T> input_vector;
function<T(T,T)> input_function;
public:
my_class(vector<T> v, function<T(T,T)> f){
input_vector = v;
input_function = f;
}
void my_function(int a, int b){
for (int i = a; i < b; i++){
input_vector[i] = input_function(input_vector[i], input_vector[i]);
}
}
void execute(){
vector<std::thread> threads;
for (int i = 0; i < 8; i++){
threads.push_back(std::thread(my_function, i, i+1));
}
for (int i = 0; i < threads.size(); i++){
threads[i].join();
}
}
};
int sum (int a, int b){
return a+b;
}
int main(){
vector<int> my_input;
for (int i = 0; i < 20; i++){
my_input.push_back(i);
}
my_class<int>* s = new my_class<int>(my_input, sum);
s->execute();
return 0;
};
我认为该错误是由于函数my_function是类成员而引起的,但是我不确定为什么。这是完整的错误消息:
In file included from .\testfile.cpp:4:0:
/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/thread: In instantiation of 'struct std::thread::_Invoker<std::tuple<void (my_class<int>::*)(int, int), int, int> >':
/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/thread:127:22: required from 'std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (my_class<int>::*)(int, int); _Args = {int&, int}]'
.\provastack.cpp:29:28: required from 'void my_class<T>::execute() [with T = int]'
.\provastack.cpp:49:13: required from here
/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/thread:240:2: error: no matching function for call to 'std::thread::_Invoker<std::tuple<void (my_class<int>::*)(int, int), int, int> >::_M_invoke(std::thread::_Invoker<std::tuple<void (my_class<int>::*)(int, int), int, int> >::_Indices)'
operator()()
^~~~~~~~
/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/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 (my_class<int>::*)(int, int), int, int>]
_M_invoke(_Index_tuple<_Ind...>)
^~~~~~~~~
/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/thread:231:4: note: template argument deduction/substitution failed:
/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/thread: In substitution of 'template<long unsigned int ..._Ind> decltype (std::__invoke(_S_declval<_Ind>()...)) std::thread::_Invoker<std::tuple<void (my_class<int>::*)(int, int), int, int> >::_M_invoke<_Ind ...>(std::_Index_tuple<_Ind1 ...>) [with long unsigned int ..._Ind = {0, 1, 2}]':
/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/thread:240:2: required from 'struct std::thread::_Invoker<std::tuple<void (my_class<int>::*)(int, int), int, int> >'
/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/thread:127:22: required from 'std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (my_class<int>::*)(int, int); _Args = {int&, int}]'
.\provastack.cpp:29:28: required from 'void my_class<T>::execute() [with T = int]'
.\provastack.cpp:49:13: required from here
/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/thread:233:29: error: no matching function for call to '__invoke(std::__tuple_element_t<0, std::tuple<void (my_class<int>::*)(int, int), int, int> >, std::__tuple_element_t<1, std::tuple<void (my_class<int>::*)(int, int), int, int> >, std::__tuple_element_t<2, std::tuple<void (my_class<int>::*)(int, int), int, int> >)'
-> decltype(std::__invoke(_S_declval<_Ind>()...))
~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/tuple:41:0,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/functional:54,
from .\provastack.cpp:1:
/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/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/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/invoke.h:89:5: note: template argument deduction/substitution failed:
/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/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 (my_class<int>::*)(int, int); _Args = {int, int}]':
/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/thread:233:29: required by substitution of 'template<long unsigned int ..._Ind> decltype (std::__invoke(_S_declval<_Ind>()...)) std::thread::_Invoker<std::tuple<void (my_class<int>::*)(int, int), int, int> >::_M_invoke<_Ind ...>(std::_Index_tuple<_Ind1 ...>) [with long unsigned int ..._Ind = {0, 1, 2}]'
/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/thread:240:2: required from 'struct std::thread::_Invoker<std::tuple<void (my_class<int>::*)(int, int), int, int> >'
/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/thread:127:22: required from 'std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (my_class<int>::*)(int, int); _Args = {int&, int}]'
.\provastack.cpp:29:28: required from 'void my_class<T>::execute() [with T = int]'
.\provastack.cpp:49:13: required from here
/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/invoke.h:89:5: error: no type named 'type' in 'struct std::__invoke_result<void (my_class<int>::*)(int, int), int, int>'