当我尝试编译以下代码时,我收到错误“无效使用不完整类型...”但我没有看到我的错误。我已经改变了包含和已定义模板的顺序。但错误仍然存在。我的代码中的“期货”有什么问题?我在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;
}
答案 0 :(得分:2)
毕竟解决方案非常简单:只需使用boost.thread即可。为什么? GCC-7.1.0没有为Windows实现“期货”。因此,MinGW也不提供此功能。存在一个外部包“mingw-std-threads”(https://github.com/meganz/mingw-std-threads),它确实提供了一些标准的线程功能,但“期货”仍然只能用于特殊的黑客攻击(参见mingw-std-threads问题#17:需要更正std :: future(可能还有std :: shared_mutex))。因此,最简单的解决方案是添加以下标头并定义BOOST_THREAD_VERSION 4(默认值为2)。我从BorisSchä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 ) );
}