C++ thread error: no type named type

倾然丶 夕夏残阳落幕 提交于 2019-12-24 06:04:10

问题


I'm trying to use threads for the first time on a method of my class.

This is a simplified version of my code that results in the same error.

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

};

In the actual code I want each thread to fill a bigger chunk of the vector.

I have this code in a Clion project with the CMakeLists.txt file:

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})

Whether I include the flag -pthread is irrelevant for the compilation, it always gives:

-- 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

I've seen many questions similar to this one but all of them didn't use the this parameter in the thread constructor or didn't use ref. My guess is I'm linking pthread wrong but its just conjecture.


回答1:


std::vector<int> values;

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

std::vector<int> and std::vector<unsigned int> are unrelated types. There is no conversion from one to another.



来源:https://stackoverflow.com/questions/40817345/c-thread-error-no-type-named-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!