observer pattern error (is not captured)

Discussion in 'C++' started by Chris Forone, Nov 12, 2012.

  1. Chris Forone

    Chris Forone Guest

    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
    Chris Forone, Nov 12, 2012
    #1
    1. Advertising

  2. Chris Forone

    SG Guest

    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
    SG, Nov 12, 2012
    #2
    1. Advertising

  3. Chris Forone

    Chris Forone Guest

    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
    Chris Forone, Nov 12, 2012
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Beatrice Rutger
    Replies:
    0
    Views:
    706
    Beatrice Rutger
    Jun 5, 2005
  2. Replies:
    6
    Views:
    730
    Chris Uppal
    Feb 13, 2006
  3. mem

    Observer pattern

    mem, May 18, 2004, in forum: C++
    Replies:
    2
    Views:
    524
    adiian
    Jun 4, 2007
  4. Paolino

    An observer pattern application.

    Paolino, Aug 18, 2005, in forum: Python
    Replies:
    0
    Views:
    333
    Paolino
    Aug 18, 2005
  5. Replies:
    10
    Views:
    189
    Anno Siegel
    Feb 9, 2006
Loading...

Share This Page