observer pattern error (is not captured)

C

Chris Forone

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
 
S

SG

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
 
C

Chris Forone

Am 12.11.2012 12:57, schrieb SG:
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
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top