C ++线程错误:无类型命名类型 [英] C++ thread error: no type named type

查看:231
本文介绍了C ++线程错误:无类型命名类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在类的方法上首次使用线程.

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.

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

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

是否包含标志-pthread与编译无关,它始终给出:

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

我已经看到了许多与此问题类似的问题,但是所有问题都没有在线程构造函数中使用this参数,也没有使用ref.我的猜测是我将pthread链接错误,但这只是一个推测.

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.

推荐答案

std::vector<int> values;

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

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

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

这篇关于C ++线程错误:无类型命名类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆