gpt4 book ai didi

C++ 线程错误 : no type named type

转载 作者:行者123 更新时间:2023-11-30 05:20:03 27 4
gpt4 key购买 nike

我第一次尝试在我的类的方法上使用线程。

这是导致相同错误的代码的简化版本。

#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;
}

};

在实际代码中,我希望每个线程填充更大的 vector block 。

我在带有 CMakeLists.txt 文件的 Clion 项目中有这段代码:

cmake_minimum_required(VERSION 3.6)
project(ThreadTest)

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_FLAGS -pthread)

set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
set(THREADS_PREFER_PTHREAD_FLAG TRUE)

set(SOURCE_FILES main.cpp foo.hpp foo.cpp)
add_executable(ThreadTest ${SOURCE_FILES})

我是否包含标志 -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

我见过很多类似这个的问题,但是都没有在线程构造函数中使用this参数或者没有使用ref。我的猜测是我将 pthread 链接错了,但这只是猜想。

最佳答案

std::vector<int> values;

... std::vector<unsigned int> &container) { ...

std::vector<int>std::vector<unsigned int>是不相关的类型。没有从一个到另一个的转换。

关于C++ 线程错误 : no type named type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40817345/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com