【问题标题】:invalid use of incomplete type using std::future使用 std::future 对不完整类型的无效使用
【发布时间】:2017-08-08 10:06:19
【问题描述】:

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

错误信息:

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

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

【问题讨论】:

  • 顺便说一句:阅读并实施来自Using STL Containers with Eigen 的建议。否则您可能会遇到随机段错误。
  • 你用什么命令编译?我手头没有 MinGW,但在 Linux 上,未来的标头有一个围绕实现的条件,建议您需要指定 -pthread
  • 命令行是:g++.exe -Wall -fexceptions -g -std=c++14 -IC:\MinGW\include -c C:\Users\user\Documents\stl_tests\main .cpp -o obj\Debug\main.o
  • 您是否也尝试在命令行上使用-pthread
  • 是的,我刚刚在命令行中添加了-pthread。但这并没有改变任何东西。

标签: c++ templates stl


【解决方案1】:

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

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-25
    • 2010-10-13
    • 2017-10-24
    • 1970-01-01
    • 2013-05-19
    • 2016-04-06
    相关资源
    最近更新 更多