[function] creating function object for member function call delegation

Hi, the following code doesn't compile: ---8<--- #include <iostream> #include <boost/function.hpp> #include <boost/bind.hpp> using namespace std; using namespace boost; struct foo { function<void ( const string& )> fn; }; struct bar { foo& m_f; bar(foo& f) : m_f( f ) { m_f.fn = bind( bar::write, this); // L20 } void write( const string& s ) { cout << s; } }; int main() { foo f; bar b( f ); f.fn( "Hello\n" ); } --->8--- class foo (object f) gets called with the string argument and shall delegate the call to the write function of bar. Writing bind( bar::write, this, _1) doesn't make things better. Here the error message: $ LANG=en g++ callback.cpp -o callback callback.cpp: In constructor 'bar::bar(foo&)': callback.cpp:20: error: no matching function for call to 'bind(<unresolved overloaded function type>, bar* const)' /usr/include/boost/bind.hpp:1383: note: candidates are: boost::_bi::bind_t<boost::_bi::unspecified, F, typename boost::_bi::list_av_1<A1>::type> boost::bind(F, A1) [with F = void (bar::*)(const std::string&), A1 = bar*] /usr/include/boost/bind.hpp:1628: note: boost::_bi::bind_t<typename boost::_bi::dm_result<M T::*, A1>::type, boost::_mfi::dm<M, T>, typename boost::_bi::list_av_1<A1>::type> boost::bind(M T::*, A1) [with A1 = bar*, M = void ()(const std::string&), T = bar] Thanks for help, Olaf

Olaf Peter skrev:
the following code doesn't compile: [SNIP] m_f.fn = bind( bar::write, this); // L20
You need to take the address of bar::write, so this should work: m_f.fn = bind( &bar::write, this); Best regards, Christian

--- On Sat, 23/8/08, Olaf Peter <ope-devel@gmx.de> wrote:
From: Olaf Peter <ope-devel@gmx.de> Subject: [Boost-users] [function] creating function object for member function call delegation To: "Boost.Org Users" <boost-users@lists.boost.org> Date: Saturday, 23 August, 2008, 11:01 AM Hi,
the following code doesn't compile:
---8<--- #include <iostream> #include <boost/function.hpp> #include <boost/bind.hpp>
using namespace std; using namespace boost;
struct foo {
function<void ( const string& )> fn; };
struct bar {
foo& m_f;
bar(foo& f) : m_f( f ) { m_f.fn = bind( bar::write, this); // L20 }
void write( const string& s ) { cout << s; } };
int main() { foo f; bar b( f ); f.fn( "Hello\n" );
} --->8---
class foo (object f) gets called with the string argument and shall delegate the call to the write function of bar. Writing bind( bar::write, this, _1) doesn't make things better.
Here the error message:
$ LANG=en g++ callback.cpp -o callback callback.cpp: In constructor 'bar::bar(foo&)': callback.cpp:20: error: no matching function for call to 'bind(<unresolved overloaded function type>, bar* const)' /usr/include/boost/bind.hpp:1383: note: candidates are: boost::_bi::bind_t<boost::_bi::unspecified, F, typename boost::_bi::list_av_1<A1>::type> boost::bind(F, A1) [with F = void (bar::*)(const std::string&), A1 = bar*] /usr/include/boost/bind.hpp:1628: note: boost::_bi::bind_t<typename boost::_bi::dm_result<M T::*, A1>::type, boost::_mfi::dm<M, T>, typename boost::_bi::list_av_1<A1>::type> boost::bind(M T::*, A1) [with A1 = bar*, M = void ()(const std::string&), T = bar]
Thanks for help, Olaf _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
Try adding the _1 placeholder: m_f.fn = bind( bar::write, this, _1); // L20

Thanks to all, adding the address operator & and _1 does the job. Thanks, Olaf
participants (3)
-
Christian Larsen
-
Olaf Peter
-
Peter Barker