Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/164.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++ 使用asio::占位符进行Boost绑定::错误_C++_Function_Boost_Bind_Boost Asio - Fatal编程技术网

C++ 使用asio::占位符进行Boost绑定::错误

C++ 使用asio::占位符进行Boost绑定::错误,c++,function,boost,bind,boost-asio,C++,Function,Boost,Bind,Boost Asio,为什么它不起作用 ---boost_bind.cc--- #包括 #包括 #包括 无效函数1(常量int&i) { } void func2(常量::asio::错误代码&e) { } int main() { ::boost::function f1=::boost::bind(&func1,1); //它不起作用! ::boost::function f2=::boost::bind(&func2,::asio::占位符::错误); 返回0; } 这就是错误: while_true@loc

为什么它不起作用

---boost_bind.cc---

#包括
#包括
#包括
无效函数1(常量int&i)
{ }
void func2(常量::asio::错误代码&e)
{ }
int main()
{
::boost::function f1=::boost::bind(&func1,1);
//它不起作用!
::boost::function f2=::boost::bind(&func2,::asio::占位符::错误);
返回0;
}
这就是错误:

while_true@localhost:~> g++ -lpthread boost_bind.cc -o boost_bind In file included from boost_bind.cc:2: /usr/include/boost/bind.hpp: In member function ‘void boost::_bi::list1::operator()(boost::_bi::type, F&, A&, int) [with F = void (*)(const asio::error_code&), A = boost::_bi::list0, A1 = boost::arg (*)()]’: /usr/include/boost/bind/bind_template.hpp:20: instantiated from ‘typename boost::_bi::result_traits::type boost::_bi::bind_t::operator()() [with R = void, F = void (*)(const asio::error_code&), L = boost::_bi::list1 (*)()>]’ /usr/include/boost/function/function_template.hpp:152: instantiated from ‘static void boost::detail::function::void_function_obj_invoker0::invoke(boost::detail::function::function_buffer&) [with FunctionObj = boost::_bi::bind_t (*)()> >, R = void]’ /usr/include/boost/function/function_template.hpp:904: instantiated from ‘void boost::function0::assign_to(Functor) [with Functor = boost::_bi::bind_t (*)()> >, R = void]’ /usr/include/boost/function/function_template.hpp:720: instantiated from ‘boost::function0::function0(Functor, typename boost::enable_if_c::type) [with Functor = boost::_bi::bind_t (*)()> >, R = void]’ /usr/include/boost/function/function_template.hpp:1040: instantiated from ‘boost::function::function(Functor, typename boost::enable_if_c::type) [with Functor = boost::_bi::bind_t (*)()> >, R = void]’ boost_bind.cc:14: instantiated from here /usr/include/boost/bind.hpp:232: error: no match for ‘operator[]’ in ‘a[boost::_bi::storage1 (*)()>::a1_ [with int I = 1]]’ while_true@localhost:~> 当_true@localhost:~>g++-lpthread boost\u bind.cc-o boost\u bind 在boost_bind.cc中包含的文件中:2: /usr/include/boost/bind.hpp:在成员函数“void boost::_bi::list1::operator()(boost::_bi::type,F&,A&,int)[带F=void(*)(const asio::error_code&),A=boost::_bi::list0,A1=boost::arg(*)()]: /usr/include/boost/bind/bind_template.hpp:20:从“typename boost::\u bi::result\u traits::type boost:::\u bi::bind\u t::operator()[带R=void,F=void(*)(const asio::error_code&),L=boost::\u bi::list1(*)()()>]” /usr/include/boost/function/function_template.hpp:152:从“静态void boost::detail::function::void_function_obj_invoker0::invoke(boost::detail::function::function_buffer&”)实例化[with FunctionObj=boost:_bi::bind_t(*)()>,R=void] /usr/include/boost/function/function_template.hpp:904:从“void boost::function0::assign_to(Functor)[with Functor=boost::_bi::bind_t(*)()()>,R=void]实例化 /usr/include/boost/function/function_template.hpp:720:从“boost::function0::function0(Functor,typename boost::enable_if_c::type)[with Functor=boost::_bi::bind_t(*)()>>,R=void]实例化 /usr/include/boost/function/function_template.hpp:1040:从“boost::function::function(Functor,typename boost::enable_if_c::type)[使用Functor=boost:::_bi::bind\u t(*)()()>,R=void]”实例化 boost_bind.cc:14:从此处实例化 /usr/include/boost/bind.hpp:232:错误:与“a[boost::\u bi::storage1(*)()()>::a1.[with int I=1]]中的“operator[]”不匹配 当_true@localhost:~>
我认为您应该编写
boost::asio::placeholders::error
,而不是
::asio::placeholders::error
。另外,我不理解您在
boost
名称空间前面的
的用途。

我认为您需要boost::system::error\u code:

void func2 (const boost::system::error_code& error)
{ }
还有:

::boost::function<void(boost::system::error_code&)> f2 = ::boost::bind (&func2, boost::asio::placeholders::error);
::boost::function f2=::boost::bind(&func2,boost::asio::placeholders::error);
我发现asio的占位符内容太冗长,所以我要写以下内容:

::boost::function<void(boost::system::error_code&)> f2 = ::boost::bind (&func2, _1);
::boost::function f2=::boost::bind(&func2,_1);

这有意义吗?--void func1(const int&i){}void func2(const boost::system::error_code&e){}boost::function f1=boost::bind(&func1,1);boost::function f2=boost::bind(&func2,asio::占位符::error);--如果第一个绑定可以工作,为什么第二个绑定不能工作?在libasio doc/asio/examples/http/client/async_client.cpp中,我们有类型asio::error_代码和参数asio::placeholders::error,它可以工作。您必须使用旧版本的boost,::boost::function<void(boost::system::error_code&)> f2 = ::boost::bind (&func2, _1);