wrote:
> [..]
> i have written this abtract class shape and its derived class circle,
> rectangle and triangle. i have also written this method for each shape
> object called 'draw_all_seq_inside2' which takes two values. i want to
> call this method using for_each statement in another method but it
> doesnt seem to work. this is a trim version of the code. i need to do
> this using for_each statement as it is a coursework. please have a look
> and get back to me (sorry for the lengthy code but i trimmed it as much
> as possible).
> [..]
Please RTFM. std::for_each is can only call a function with one argument
and that argument should be the result of dereferencing the iterator (or
one convertible from the result of dereferencing the iterator). IOW, one
possible implementation of 'std::for_each' is this:
template<class I, class F>
F my_for_each(I i1, I i2, F f)
{
while (i1 != i2)
f(*i1++);
return f;
}
If you read this and try to understand what it does, you will see that you
need to supply a _function pointer_ or a _functor_ as the third argument
of 'for_each'. 'mem_fun' is an adapter. It takes a _no-argument_ member
or a _single-argument_ member function and makes a one-argument functor
or a two-argument functor out of it. Yours is a _two-argument_ member
function (which really has three arguments, the first is the hidden object
pointer or reference which inside becomes 'this'). If you want to use
'mem_fun' with it, you have to also use 'bind2nd' with it to pass the
second argument of your member function. That makes the example more
complex than you're ready to tackle. Study more about templates and about
'bind1st' and 'bind2nd' binders and 'mem_fun*' adapters.
If somebody writes it for you, I bet you're not really going to learn much
about those mechanisms. If you want to learn, you have to do it yourself.
There are plenty of examples of using 'bind1st' and 'bind2nd' and also of
'mem_fun' in the archives. I could write another one, but I don't really
see the point.
V