使用 std::future 无效使用不完整类型 [英] invalid use of incomplete type using std::future

查看:75
本文介绍了使用 std::future 无效使用不完整类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试编译以下代码时收到错误不完整类型的无效使用...",但我没有看到我的错误.我已经更改了包含和定义模板的顺序.但错误仍然存​​在.我的代码中的期货"有什么问题?我在 Windows 10 上使用 gcc-7.1.0.

I get the error "invalid use of incomplete type ..." when I try to compile the following code but I do not see my mistake. I already changed the order of includes and of the defined templates. But the error remains. What is wrong with the "futures" in my code? I am using gcc-7.1.0 on Windows 10.

错误信息:

C:\Users\user\Documents\stl_tests\template_tests.h|60|error: invalid use of incomplete type 'class std::future<std::pair<short unsigned int, std::vector<Eigen::Matrix<float, 3, 1>, std::allocator<Eigen::Matrix<float, 3, 1> > > > >'|    
c:\mingw\include\c++\7.1.0\future|125|note: declaration of 'class std::future<std::pair<short unsigned int, std::vector<Eigen::Matrix<float, 3, 1>, std::allocator<Eigen::Matrix<float, 3, 1> > > > >'|

标题template_tests.h":

Header "template_tests.h":

#ifndef TEMPLATE_TESTS_H_INCLUDED
#define TEMPLATE_TESTS_H_INCLUDED

#include <iostream>
#include <vector>
#include <utility>
#include <future>

#include <eigen3/Eigen/Dense>

template< typename T >
using Point3D = Eigen::Matrix< T, 3, 1 >;

template< typename T >
using PointSet3D = std::vector< Point3D< T > >;

template< typename T >
using BinnedPointSet3D = std::pair< unsigned short, PointSet3D< T > >;

template< typename T >
class PointSearch
{
public:
    PointSearch();
    ~PointSearch();

    void Run();

private:
    BinnedPointSet3D< T > RunThread( unsigned short );
    std::vector< BinnedPointSet3D< T > > SelectRegionData3D( unsigned short );
};

template< typename T >
BinnedPointSet3D< T > PointSearch< T >::RunThread( unsigned short binNumber )
{
    PointSet3D< T >     points;
    Point3D< T >        pt;

    for(int i = 0; i < 200; i++)
    {
        points.emplace_back(pt.Random());
    }

    return std::make_pair( binNumber, move(points) );
}

template< typename T >
std::vector< BinnedPointSet3D< T > > PointSearch< T >::SelectRegionData3D( unsigned short bins )
{
    std::vector< std::future< BinnedPointSet3D< T > > >  futures;

    for ( unsigned short i = 0; i < bins; i++ )
    {
        futures.push_back( async( std::launch::async, &PointSearch::RunThread, this, i ) );
    }

    std::vector< BinnedPointSet3D< T > >    allPoints3D;

    for ( auto &result : futures )
    {
        allPoints3D.emplace_back( result.get() );
    }

    return allPoints3D;
}

template< typename T >
void PointSearch< T >::Run()
{
    try
    {
        for(unsigned short bin = 0; bin < 128; bin++)
        {
            SelectRegionData3D(bin);
        }
    }
    catch ( ... )
    {
        std::cerr << "I don't know what went wrong..." << std::endl;
    }
}

#endif // TEMPLATE_TESTS_H_INCLUDED

主 C++ 文件:

#include "template_tests.h"

int main()
{
    PointSearch< float >    ps;

    ps.Run();

    return 0;
}

推荐答案

毕竟解决方案很简单:使用 boost.thread 即可.为什么?GCC-7.1.0 没有为 Windows 实现未来".因此,MinGW 也不提供此功能.存在一个外部包mingw-std-threads"(https://github.com/meganz/mingw-std-threads) 确实提供了一些标准的线程功能,但期货"仍然只能通过特殊的 hack 使用(参见 mingw-std-threads 问题 #17:需要更正 std::future(和可能是 std::shared_mutex) ).因此,最简单的解决方案是添加以下标头并定义 BOOST_THREAD_VERSION 4(默认值为 2).我从 Boris Schäling 那里得到的提示 (https://theboostcpplibraries.com):

After all the solution is very simple: just use boost.thread. Why? GCC-7.1.0 does not implement "futures" for Windows. So, MinGW does not provide this feature as well. There exists an external package "mingw-std-threads" (https://github.com/meganz/mingw-std-threads) which does provide some standard threading features but "futures" are still usable only with a special hack (see mingw-std-threads issue #17: Need correction for std::future (and probably std::shared_mutex) ). Therefore the easiest solution was to add the following headers and to define BOOST_THREAD_VERSION 4 (default is 2). This hint I got from Boris Schäling (https://theboostcpplibraries.com):

#define BOOST_THREAD_VERSION 4
#include <boost/thread.hpp>
#include <boost/thread/future.hpp>

在其余代码中,我只需要在几个地方从 std 切换到 boostina:

In the rest of the code I only had to switch from std to boostina few places:

std::vector< boost::future< BinnedPointSet3D< T > > >  futures;

for ( unsigned short i = 0; i < bins; i++ )
{
    futures.push_back( boost::async( boost::launch::async, &PointSearch::RunThread, this, i ) );
}

这篇关于使用 std::future 无效使用不完整类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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