Boost logo

Boost Users :

From: Ioffe, Robert (robert.ioffe_at_[hidden])
Date: 2003-08-12 11:17:49


Dirk,

The following should not compile:

for_each(v.begin(), v.end(), boost::bind(&myclass::print, _1));

since myclass::print actually takes two arguments and the first one is
not the std::string, but the object of class myclass.

So you should do something like this:

myclass foo;
for_each(v.begin(), v.end(), boost::bind(&myclass::print, foo, _1));

Have fun,
Robert Ioffe

-----Original Message-----
From: dirk_griffioen [mailto:diddekio_at_[hidden]]
Sent: Tuesday, August 12, 2003 3:43 AM
To: Boost-Users_at_[hidden]
Subject: [Boost-Users] boos::bind class member function

Hi,

I would like to use a for_each with a class member function.

The for_each in myclass::do_print below does not compile. And I
simply cant figure it out.

I'm using vc7.1.

////////////////////////////////
void print2(std::string s)
{
  cout << s << '\n';
}

class myclass
{
public:
  void print(std::string s);
  void do_print();
};

void myclass::print(std::string s)
{
  cout << s << '\n';
}

void myclass::do_print()
{
  vector<std::string> v;
  v.push_back("a");
  v.push_back("b");
  v.push_back("c");
  v.push_back("d");
  
  for_each(v.begin(), v.end(),
           boost::bind(boost::type<void>(),
           &myclass::print, _1));
}
/////////////////////////////////////////

nor will

for_each(v.begin(), v.end(), boost::bind(&myclass::print, _1));
for_each(v.begin(), v.end(), boost::bind(print, _1));

whereas

for_each(v.begin(), v.end(), boost::bind(print2, _1));

is ok

It seems natural to do, but I cant get it.

Dirk

Info: <http://www.boost.org>
Wiki: <http://www.crystalclearsoftware.com/cgi-bin/boost_wiki/wiki.pl>
Unsubscribe: <mailto:boost-users-unsubscribe_at_[hidden]>
 

Your use of Yahoo! Groups is subject to
http://docs.yahoo.com/info/terms/


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net