2010-04-26 108 views
1

爲什麼它不起作用?Boost與asio ::佔位符綁定::錯誤

--- boost_bind.cc ---

#include <asio.hpp> 
#include <boost/bind.hpp> 
#include <boost/function.hpp> 

void func1 (const int& i) 
{ } 

void func2 (const ::asio::error_code& e) 
{ } 

int main() 
{ 
    ::boost::function<void()> f1 = ::boost::bind (&func1, 1); 

    // it doesn't work! 
    ::boost::function<void()> f2 = ::boost::bind (&func2, ::asio::placeholders::error); 

    return 0; 
} 

這是錯誤:

 
[email protected]:~> 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]]’ 
[email protected]:~> 

回答

2

我想你應該寫boost::asio::placeholders::error而不是::asio::placeholders::error。另外,我不明白::您在boost命名空間前的目的。

0

我想你想的boost ::系統:: ERROR_CODE:

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

而且這樣的:

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

我覺得ASIO的佔位符的東西太冗長,所以我會寫這個:

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

它有道理嗎? --- 空隙的func1(const int的&I){} 空隙FUNC2(常量升壓::系統:: ERROR_CODE&E){} 升壓::功能 F1 =升壓::綁定(func1的,1); boost :: function f2 = boost :: bind(&func2,asio :: placeholders :: error); --- 如果第一次綁定起作用,爲什麼第二次不起作用?在libasio-doc/asio/examples/http/client/async_client.cpp中使用 – Leandro 2010-04-27 00:28:24

+0

我們有類型asio :: error_code和參數asio :: placeholders :: error,它可以工作。 – Leandro 2010-04-27 00:32:00

+0

您必須使用舊版本的boost, ergosys 2010-04-27 02:56:48