Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > observer pattern error (is not captured)

Reply
Thread Tools

observer pattern error (is not captured)

 
 
Chris Forone
Guest
Posts: n/a
 
      11-12-2012
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
 
Reply With Quote
 
 
 
 
SG
Guest
Posts: n/a
 
      11-12-2012
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

 
Reply With Quote
 
 
 
 
Chris Forone
Guest
Posts: n/a
 
      11-12-2012
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Observer pattern mem C++ 2 06-04-2007 10:32 PM
Observer pattern issue cstratelos@gmail.com Java 6 02-13-2006 12:43 PM
weakrefs to functions for observer pattern Michael Schneider Python 3 11-03-2005 03:24 PM
An observer pattern application. Paolino Python 0 08-18-2005 08:24 AM
JMS or MVC (Observer pattern) in lightweight front end? Beatrice Rutger Java 0 06-05-2005 04:27 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57