
Hello, Can someone please tell me why this doesn't work? #include <boost/bind.hpp> #include <boost/function.hpp> #include <boost/shared_ptr.hpp> using namespace std; using namespace boost; struct MyClass { int i; void foo(int j){ printf("%d, %d\n", i, j); } function<void(int)> bar() { return bind<void>(&MyClass::foo, this); } }; $ g++ -c -g -I/usr/local/include/boost-1_33_1 -I.. foo.cpp /usr/local/include/boost-1_33_1/boost/mem_fn.hpp: In member function 'R& boost::_mfi::dm<R, T>::operator()(T*) const [with R = void () (int), T = MyClass]': /usr/local/include/boost-1_33_1/boost/bind.hpp:229: instantiated from 'void boost::_bi::list1<A1>::operator()(boost::_bi::type<void>, F&, A&, int) [with F = void (MyClass::*)(int), A = boost::_bi::list1<int&>, A1 = boost::_bi::value<MyClass*>]' /usr/local/include/boost-1_33_1/boost/bind/bind_template.hpp:32: instantiated from 'typename boost::_bi::result_traits<R, F>::type boost::_bi::bind_t<R, F, L>::operator()(A1&) [with A1 = int, R = void, F = void (MyClass::*)(int), L = boost::_bi::list1<boost::_bi::value<MyClass*> >]' /usr/local/include/boost-1_33_1/boost/function/function_template.hpp: 136: instantiated from 'static void boost::detail::function::void_function_obj_invoker1<FunctionObj, R, T0>::invoke(boost::detail::function::any_pointer, T0) [with FunctionObj = boost::_bi::bind_t<void, void (MyClass::*)(int), boost::_bi::list1<boost::_bi::value<MyClass*> > >, R = void, T0 = int]' /usr/local/include/boost-1_33_1/boost/function/function_template.hpp: 479: instantiated from 'void boost::function1<R, T0, Allocator>::assign_to(FunctionObj, boost::detail::function::function_obj_tag) [with FunctionObj = boost::_bi::bind_t<void, void (MyClass::*)(int), boost::_bi::list1<boost::_bi::value<MyClass*> > >, R = void, T0 = int, Allocator = std::allocator<void>]' /usr/local/include/boost-1_33_1/boost/function/function_template.hpp: 430: instantiated from 'void boost::function1<R, T0, Allocator>::assign_to(Functor) [with Functor = boost::_bi::bind_t<void, void (MyClass::*)(int), boost::_bi::list1<boost::_bi::value<MyClass*> > >, R = void, T0 = int, Allocator = std::allocator<void>]' /usr/local/include/boost-1_33_1/boost/function/function_template.hpp: 294: instantiated from 'boost::function1<R, T0, Allocator>::function1(Functor, typename boost::enable_if_c<boost::type_traits::ice_not<boost::is_integral<Functo r>::value>::value, int>::type) [with Functor = boost::_bi::bind_t<void, void (MyClass::*)(int), boost::_bi::list1<boost::_bi::value<MyClass*> > >, R = void, T0 = int, Allocator = std::allocator<void>]' /usr/local/include/boost-1_33_1/boost/function/function_template.hpp: 637: instantiated from 'boost::function<R ()(T0), Allocator>::function(Functor, typename boost::enable_if_c<boost::type_traits::ice_not<boost::is_integral<Functo r>::value>::value, int>::type) [with Functor = boost::_bi::bind_t<void, void (MyClass::*)(int), boost::_bi::list1<boost::_bi::value<MyClass*> > >, R = void, T0 = int, Allocator = std::allocator<void>]' foo.cpp:15: instantiated from here /usr/local/include/boost-1_33_1/boost/mem_fn.hpp:347: error: invalid use of non-static member function Thanks, Sean -- Compassion, tolerance, consideration of others, the responsible use of knowledge and power. These principles transcend the barriers between religious believers and non-believers; they belong not to one faith, but to all faiths. -- The 14th Dalai Lama

Sean Rhea wrote:
Hello,
Can someone please tell me why this doesn't work?
#include <boost/bind.hpp> #include <boost/function.hpp> #include <boost/shared_ptr.hpp>
using namespace std; using namespace boost;
struct MyClass { int i; void foo(int j){ printf("%d, %d\n", i, j); } function<void(int)> bar() { return bind<void>(&MyClass::foo, this); } };
Unlike some other libraries, bind does not automatically append incoming arguments to the call; if you say bind( f, 5 ), it calls f( 5 ), regardless of how many input arguments are supplied. If you want the first input argument to be passed to MyClass::foo, you need to mention it explicitly, using _1: bind( &MyClass::foo, this, _1 )

On Feb 13, 2006, at 6:28 PM, Peter Dimov wrote:
Unlike some other libraries, bind does not automatically append incoming arguments to the call; if you say bind( f, 5 ), it calls f( 5 ), regardless of how many input arguments are supplied. If you want the first input argument to be passed to MyClass::foo, you need to mention it explicitly, using _1:
bind( &MyClass::foo, this, _1 )
Ah, right. Thanks, Sean -- [P]aramount among the responsibilities of a free press is the duty to prevent any part of the government from deceiving the people and sending them off to distant lands to die of foreign fevers and foreign shot and shell. -- U.S. Supreme Court Justice Hugo Black
participants (2)
-
Peter Dimov
-
Sean Rhea