find_if and mem_fun not getting expected results

Discussion in 'C++' started by jonnyothan@gmail.com, Jan 15, 2008.

  1. Guest

    The following code isn't producing expected results:

    class Selectable
    {
    bool IsSelected() const;
    };

    class Unit : public Selectable
    {
    };

    typedef std::list<Unit *> UnitList;
    UnitList * units = mapSurface->GetUnits();

    UnitList::iterator selectedUnit = find_if(units->begin(), units-
    >end(), mem_fun(&Unit::IsSelected));


    When no units are selected, find_if is still returning the first (and
    only, in my test case) unit. I am even stepping through the calls to
    mem_fun and IsSelected and it is returning false! What's even
    stranger, the following code did the right thing:

    UnitList::iterator selectedUnit = units->begin();
    while(selectedUnit!=units->end())
    {
    if ((*selectedUnit)->IsSelected())
    {
    break;
    }
    ++selectedUnit;
    }

    What am I doing wrong?!?

    Thanks in advance!
     
    , Jan 15, 2008
    #1
    1. Advertising

  2. Guest

    Turns out it was a compiler error. I'm not great at x86, but the
    IsSelected function was only setting the low byte of eax to 0. Then
    when it returned, eax was converted to 1 because the higher 3 bytes
    had a non-zero value.

    Changing IsSelected to return int fixed the problem.

    This was using MSVC 7.1, by the way (I know, it's ancient).
     
    , Jan 15, 2008
    #2
    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. k0tic
    Replies:
    1
    Views:
    590
    Alf P. Steinbach
    Sep 4, 2003
  2. marco_segurini
    Replies:
    3
    Views:
    2,315
    Karl Heinz Buchegger
    Jul 7, 2004
  3. Mr X
    Replies:
    9
    Views:
    644
    Mike Wahler
    Jan 19, 2005
  4. Noah Roberts

    bind2nd, mem_fun, and references

    Noah Roberts, Jul 15, 2006, in forum: C++
    Replies:
    8
    Views:
    839
    P.J. Plauger
    Jul 15, 2006
  5. cinsk
    Replies:
    1
    Views:
    351
    cinsk
    Oct 18, 2010
Loading...

Share This Page