On 12 Mai, 17:28, Nikola <popiz...@[EMAIL PROTECTED]
> wrote:
> class Simple
> {
>
> };
>
> class Holder
> {
>
> vector<Simple> vec;
>
> void Do(const Simple& s) const
> {
> // does something with s
> }
>
> void DoAll()
> {
> for_each(vec.begin(), vec.end(), mem_fun_ref(&Holder::Do));
> }
>
> };
>
> Here is what I get: term does not evaluate to a function taking 1
> arguments
Problem is, that the used overload of mem_fun_ref is
a *binary* functor, it expects as it's first argument
a reference (therefore the name) to the class to which
the pointer to member function belongs (i.e. to Holder)
and it expects as the second argument the parameter
that shall be provided as member function argument (i.e.
a const Simple&). In contrast to this, std::for_each
expects a function(-like object) to which only one
argument is provided (In this case: Each Simple object
of the container). To fix this signature mismatch, you
can use another functor adaptor, std::bind1st, which
does the job you need:
for_each(vec.begin(), vec.end(),
bind1st(mem_fun_ref(&Holder::Do), *this));
Now the actual functor provided to for_each expects
only one argument (const Simple&) and ensures that
*this is bound to the first argument of mem_fun_ref,
which solves your problem.
HTH & Greetings from Bremen,
Daniel Krügler
--
[ See http://www.gotw.ca/resources/clcm.htm
for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


|