gpt4 book ai didi

c++ - 使用 std::future 无效使用不完整类型

转载 作者:搜寻专家 更新时间:2023-10-31 02:10:16 24 4
gpt4 key购买 nike

当我尝试编译以下代码时收到错误“不完整类型的无效使用...”,但我没有看到我的错误。我已经更改了包含和定义模板的顺序。但错误仍然存​​在。我代码中的“ future ”有什么问题?我在 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> > > > >'|

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 实现“futures”。所以,MinGW 也不提供这个功能。存在一个外部包“mingw-std-threads”(https://github.com/meganz/mingw-std-threads),它确实提供了一些标准的线程功能,但“futures”仍然只能用于特殊的 hack(参见 mingw-std-threads 问题 #17:需要更正std::future(可能还有 std::shared_mutex))。因此,最简单的解决方案是添加以下 header 并定义 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 ) );
}

关于c++ - 使用 std::future 无效使用不完整类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45565412/

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