C++ C++;线程错误:没有名为type的类型
我第一次尝试在我的类的方法上使用线程 这是我的代码的简化版本,会导致相同的错误C++ C++;线程错误:没有名为type的类型,c++,multithreading,methods,pthreads,C++,Multithreading,Methods,Pthreads,我第一次尝试在我的类的方法上使用线程 这是我的代码的简化版本,会导致相同的错误 #include <thread> #include <iostream> #include <memory> #include <vector> class Foo{ public: std::vector<int> DoThing() { return {1}; } std::vector<int>
#include <thread>
#include <iostream>
#include <memory>
#include <vector>
class Foo{
public:
std::vector<int> DoThing() {
return {1};
}
std::vector<int> DoThingMultiThread(unsigned int threads) {
std::vector<int> values;
std::vector<std::thread> threadVec(threads);
for (unsigned int i = 0; i < threads; ++i)
{
threadVec.at(i) = std::thread(&Foo::DoPartialThing, this, i, i, std::ref(values));
}
return values;
}
private:
void DoPartialThing(unsigned int value, unsigned int position, std::vector<unsigned int> &container) {
container.at(position) = value;
}
};
我是否包含标志-pthread与编译无关,它总是给出:
-- Configuring done
-- Generating done
-- Build files have been written to: /home/mjgalindo/ClionProjects/ThreadTest/cmake-build-debug
[ 33%] Building CXX object CMakeFiles/ThreadTest.dir/main.cpp.o
[ 66%] Building CXX object CMakeFiles/ThreadTest.dir/foo.cpp.o
In file included from /usr/include/c++/6/thread:39:0,
from /home/mjgalindo/ClionProjects/ThreadTest/foo.cpp:3:
/usr/include/c++/6/functional: In instantiation of ‘struct std::_Bind_simple<std::_Mem_fn<void (Foo::*)(unsigned int, unsigned int, std::vector<unsigned int>&)>(Foo*, unsigned int, unsigned int, std::reference_wrapper<std::vector<int> >)>’:
/usr/include/c++/6/thread:137:26: required from ‘std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (Foo::*)(unsigned int, unsigned int, std::vector<unsigned int>&); _Args = {Foo*, unsigned int&, unsigned int&, std::reference_wrapper<std::vector<int, std::allocator<int> > >}]’
/home/mjgalindo/ClionProjects/ThreadTest/foo.cpp:15:89: required from here
/usr/include/c++/6/functional:1374:61: error: no type named ‘type’ in ‘class std::result_of<std::_Mem_fn<void (Foo::*)(unsigned int, unsigned int, std::vector<unsigned int>&)>(Foo*, unsigned int, unsigned int, std::reference_wrapper<std::vector<int> >)>’
typedef typename result_of<_Callable(_Args...)>::type result_type;
^~~~~~~~~~~
/usr/include/c++/6/functional:1395:9: error: no type named ‘type’ in ‘class std::result_of<std::_Mem_fn<void (Foo::*)(unsigned int, unsigned int, std::vector<unsigned int>&)>(Foo*, unsigned int, unsigned int, std::reference_wrapper<std::vector<int> >)>’
_M_invoke(_Index_tuple<_Indices...>)
^~~~~~~~~
CMakeFiles/ThreadTest.dir/build.make:86: recipe for target 'CMakeFiles/ThreadTest.dir/foo.cpp.o' failed
make[3]: *** [CMakeFiles/ThreadTest.dir/foo.cpp.o] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/ThreadTest.dir/all' failed
make[2]: *** [CMakeFiles/ThreadTest.dir/all] Error 2
CMakeFiles/Makefile2:79: recipe for target 'CMakeFiles/ThreadTest.dir/rule' failed
make[1]: *** [CMakeFiles/ThreadTest.dir/rule] Error 2
Makefile:118: recipe for target 'ThreadTest' failed
make: *** [ThreadTest] Error 2
——配置完成
--生成完成
--生成文件已写入:/home/mjgalindo/ClionProjects/ThreadTest/cmake Build debug
[33%]构建CXX对象cmakFiles/ThreadTest.dir/main.cpp.o
[66%]构建CXX对象cmakFiles/ThreadTest.dir/foo.cpp.o
在/usr/include/c++/6/thread:39:0中包含的文件中,
from/home/mjgalindo/ClionProjects/ThreadTest/foo.cpp:3:
/usr/include/c++/6/functional:在“struct std::_Bind_simple”的实例化中:
/usr/include/c++/6/thread:137:26:std::thread(_Callable&&,_Args&&&…[with _Callable=void(Foo::*)(unsigned int,unsigned int,std::vector&);_Args={Foo*,unsigned int&,std::reference_wrapper}中的必需参数
/home/mjgalindo/ClionProjects/ThreadTest/foo.cpp:15:89:此处需要
/usr/include/c++/6/functional:1374:61:错误:在“class std::result_of”中没有名为“type”的类型
typedef typename result_of::type result_type;
^~~~~~~~~~~
/usr/include/c++/6/functional:1395:9:错误:在“class std::result\u of”中没有名为“type”的类型
_M_invoke(_Index_tuple)
^~~~~~~~~
CMakeFiles/ThreadTest.dir/build.make:86:目标“CMakeFiles/ThreadTest.dir/foo.cpp.o”的配方失败
生成[3]:***[CMakeFiles/ThreadTest.dir/foo.cpp.o]错误1
CMakeFiles/Makefile2:67:目标“CMakeFiles/ThreadTest.dir/all”的配方失败
生成[2]:***[CMakeFiles/ThreadTest.dir/all]错误2
CMakeFiles/Makefile2:79:目标“CMakeFiles/ThreadTest.dir/rule”的配方失败
生成[1]:***[CMakeFiles/ThreadTest.dir/rule]错误2
Makefile:118:目标“ThreadTest”的配方失败
make:**[ThreadTest]错误2
我见过许多与此类似的问题,但它们都没有在线程构造函数中使用this参数或没有使用ref。我猜我链接pthread是错误的,但这只是猜测。std::vector values;
std::vector<int> values;
... std::vector<unsigned int> &container) { ...
... 向量和容器){。。。
std::vector
和std::vector
是不相关的类型。两者之间没有转换。愚蠢的问题,真正的代码没有相同的问题,因此它最终是不相关的……不过,错误消息在这两种情况下都是相同的,即使解决了这个问题,也不起作用“在没有活动异常的情况下终止调用”,这应该是另一个问题的主题。皮带和吊杆编程:代码创建一个大小为threads
的向量,运行for
循环,该循环从0开始,在索引为threads
时退出,以防万一,使用at()
检查索引是否在边界内。@PeteBecker你的意思是说在这种情况下at()是多余的?我一直使用[],直到我发现at()更安全。是的,at()是多余的。
只是“更安全”“如果它添加了一些重要的内容,而这些内容并不存在。正确写入的循环将索引索引到向量中,并且检查索引是否有效不会向已经存在的内容添加任何内容。如果正确写入循环,则很少需要额外的检查。”。
std::vector<int> values;
... std::vector<unsigned int> &container) { ...