for_each, mem_fun, bind1st with boost::ptr_vector?

Hi, I can't figure out how to use std::mem_fun() and std::bind1st() with std::for_each() to call into a member function for each element in a boost::ptr_vector object. Some sample code below: class Object { public: void Tick( float number ) {} }; boost::ptr_vector<Object> m_objects; using namespace std; for_each( m_objects.begin(), m_objects.end(), bind1st( mem_fun( &Object::Tick ), 5 ) ); Of course, this doesn't compile at all. Can anyone tell me what I'm doing wrong?

I can't figure out how to use std::mem_fun() and std::bind1st() with std::for_each() to call into a member function for each element in a boost::ptr_vector object. Some sample code below: class Object { public: void Tick( float number ) {} }; boost::ptr_vector<Object> m_objects; using namespace std; for_each( m_objects.begin(), m_objects.end(), bind1st( mem_fun( &Object::Tick ), 5 ) ); Of course, this doesn't compile at all. Can anyone tell me what I'm doing wrong? Try using boost::bind. It makes life so much simpler. Once I discovered that I never used bind1st, mem_fun, etc. again. I believe this is included in TR1 anyway. for_each( m_objects.begin(), m_objects.end(), boost::bind(&Object::Tick, _1, 5.0) ); Although, this may give you a warning about converting a double to a float. Why is Tick defined to take a float anyway? -- Bill --

I ended up using boost::bind() anyway lol.
Tick is a function called in my game every frame. Tick takes a float because
my game uses seconds as delta time. 1.0f is 1 second, 0.5f is half a second
and so on.
Thanks for your help.
On Nov 27, 2007 4:21 PM, Bill Buklis
I can't figure out how to use std::mem_fun() and std::bind1st() with std::for_each() to call into a member function for each element in a boost::ptr_vector object. Some sample code below:
class Object { public: void Tick( float number ) {} };
boost::ptr_vector<Object> m_objects; using namespace std; for_each( m_objects.begin(), m_objects.end(), bind1st( mem_fun( &Object::Tick ), 5 ) );
Of course, this doesn't compile at all. Can anyone tell me what I'm doing wrong?
Try using boost::bind. It makes life so much simpler. Once I discovered that I never used bind1st, mem_fun, etc. again. I believe this is included in TR1 anyway.
for_each( m_objects.begin(), m_objects.end(), boost::bind(&Object::Tick, _1, 5.0) );
Although, this may give you a warning about converting a double to a float. Why is Tick defined to take a float anyway?
-- Bill --
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (2)
-
Bill Buklis
-
Robert Dailey