Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;,Linux:错误:从‘;boost::独特的未来<;无效>’;到非标量类型‘;boost::共享未来<;无效>’;请求。如何避开它?_C++_Boost_Scheduled Tasks_Boost Thread_Future - Fatal编程技术网

C++ C++;,Linux:错误:从‘;boost::独特的未来<;无效>’;到非标量类型‘;boost::共享未来<;无效>’;请求。如何避开它?

C++ C++;,Linux:错误:从‘;boost::独特的未来<;无效>’;到非标量类型‘;boost::共享未来<;无效>’;请求。如何避开它?,c++,boost,scheduled-tasks,boost-thread,future,C++,Boost,Scheduled Tasks,Boost Thread,Future,我试着和你一起工作。如图所示,我们可以从 所以我在linux上尝试了这样的功能: template <class task_return_t> void pool_item( boost::shared_ptr< boost::packaged_task<task_return_t> > pt) { boost::shared_future<task_return_t> fi= pt->get_future(); // error

我试着和你一起工作。如图所示,我们可以从

所以我在linux上尝试了这样的功能:

template <class task_return_t>
void pool_item( boost::shared_ptr< boost::packaged_task<task_return_t> > pt)
{
    boost::shared_future<task_return_t> fi= pt->get_future(); // error
    //...
模板
无效池_项(boost::shared_ptrpt)
{
boost::shared_future fi=pt->get_future();//错误
//...
但我把它叫做:

../../src/cf-util/thread_pool.h: In member function ‘void thread_pool::pool_item(boost::shared_ptr<boost::packaged_task<R> >) [with task_return_t = void]’:
../../src/cf-util/thread_pool.h:64:3:   instantiated from ‘void thread_pool::post(boost::shared_ptr<boost::packaged_task<R> >) [with task_return_t = void]’
../../src/cf-server/server.cpp:39:27:   instantiated from here
../../src/cf-util/thread_pool.h:124:58: error: conversion from ‘boost::unique_future<void>’ to non-scalar type ‘boost::shared_future<void>’ requested
。/../src/cf util/thread\u pool.h:在成员函数“void thread\u pool::pool\u item(boost::shared\u ptr)[with task\u return\u t=void]中:
../../src/cf util/thread\u pool.h:64:3:从“void thread\u pool::post(boost::shared\u ptr)[with task\u return\u t=void]实例化”
../../src/cf server/server.cpp:39:27:从此处实例化
../../src/cf util/thread\u pool.h:124:58:错误:请求从“boost::unique\u future”转换为非标量类型“boost::shared\u future”
我以前没有从这个任务中获得任何未来。在VisualStudio2010下的Windows上,它可以完美地编译和工作


我该怎么办?如何修复或避免此错误?

是否可能您忘记包含正确的标题

如果没有,下面的构造函数是否可以工作

boost::shared_future<task_return_t> fi(pt->get_future());
boost::共享未来fi(pt->get_future());

唯一未来
共享未来
的转换使用以下构造函数:


在C++11中,您应该调用std::future::share()从未来获取共享的未来。例如

auto sf=std::async([]{/* something */}).share();

Jeffrey Yasskin是正确的。该函数在Linux上使用g++(4.6.3)编译时使用-std=gnu++0x标志,或者如果您希望严格执行ISO编译,则使用-std=c++0x标志。这些-std标志现在分别被弃用为gnu++11和c++11。(我刚刚对此进行了评论,但还没有足够的特权)

可能是版本问题?我可以将
唯一的未来
转换为
共享的未来
我就可以了。@Luc Danton:告诉我怎么做,我会试试看!)第二个问题是我将任务传递给我的任务管理器,然后将tham发送到此函数中。所以这里的问题是
唯一的未来
可以已经被接受\分配并重新分配例如,打开
.wait();
。就像您正在尝试的那样:
boost::shared_future f=task.get_future();
其中
task
boost::packated_task;
(您的第二个问题听起来像是需要一个单独的问题。)-std=gnu++11仅在gcc-4.7及以上版本和clang-3.0或-3.1及以上版本中受支持,因此我还没有决定是使用它还是使用“0x”拼写。从长远来看,是的,将首选“11”拼写。
boost::shared_future<task_return_t> fi = boost::move(pt->get_future());
auto sf=std::async([]{/* something */}).share();