Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

observer pattern error (is not captured)

49 views
Skip to first unread message

Chris Forone

unread,
Nov 12, 2012, 6:06:21 AM11/12/12
to
hello group,

in my subject-methode detach(const observer& o) i used following line to
remove the observer:

observers.remove_if([](MyObserver mo){ return &o == &mo; });

gcc 4.7 (mingw) give me the error: 'mo' is not captured. what does this
mean and how can i achieve the desired behavior?

thanks a lot, cheers chris

SG

unread,
Nov 12, 2012, 6:57:03 AM11/12/12
to
Am 12.11.2012 12:06, schrieb Chris Forone:
>
> in my subject-methode detach(const observer& o) i used following line to
> remove the observer:
>
> observers.remove_if([](MyObserver mo){ return &o == &mo; });
>
> gcc 4.7 (mingw) give me the error: 'mo' is not captured. what does this
> mean and how can i achieve the desired behavior?

Please post real code the next time.

First of all, the function parameter is passed by value which is
probably NOT what you want. Secondly, I believe that you named your
function parameter "o" instead of "mo". Then, the error messages
actually makes sense because the observer you want to delete is not
known to the lambda because you havn't captured this information.

I am GUESSING (because you did not post the relevant information) that
you should have written

observers.remove_if([&mo](MyObserver const& o){return &o == &mo;});

I'm guessing observers is some kind of list and somehow you got hold of
reference (mo) to the list element you want to remove. How about
changing this to an iterator instead? Then, you could just remove it
like this:

list<MyObserver>::iterator mo_iter = ...;
:::
observers.erase(mo_iter);

and get rid of the linear search (remove_if).

Cheers!
SG

Chris Forone

unread,
Nov 12, 2012, 8:17:13 AM11/12/12
to
ah, so its a kind of visibility/scope problem. the var in the square
bracket is local to the lambda?

thanks a lot, cheers, chris
0 new messages