Iterator failure while moving over equal_range in Boost MultiIndex container

Posted by Sarah on Stack Overflow See other posts from Stack Overflow or by Sarah
Published on 2010-04-22T18:52:27Z Indexed on 2010/04/22 18:53 UTC
Read the original article Hit count: 354

Filed under:
|
|

I'm making some mistake with my iterators, but I can't see it yet.

I have a Boost MultiIndex container, HostContainer hmap, whose elements are boost::shared_ptr to members of class Host. All the indices work on member functions of class Host. The third index is by Host::getHousehold(), where the household member variable is an int.

Below, I'm trying to iterate over the range of Hosts matching a particular household (int hhold2) and load the corresponding private member variable Host::id into an array. I'm getting an "Assertion failed: (px != 0), function operator->, file /Applications/boost_1_42_0/boost/smart_ptr/shared_ptr.hpp, line 418" error in runtime when the household size is 2. (I can't yet tell if it happens anytime the household size is 2, or if other conditions must be met.)

typedef multi_index_container<
  boost::shared_ptr< Host >,
  indexed_by< 
    hashed_unique< const_mem_fun<Host,int,&Host::getID> >, // 0 - ID index
    ordered_non_unique< const_mem_fun<Host,int,&Host::getAgeInY> >, // 1 - Age index
    ordered_non_unique< const_mem_fun<Host,int,&Host::getHousehold> > // 2 - Household index
    > // end indexed_by
  > HostContainer; 

typedef HostContainer::nth_index<2>::type HostsByHH;

// inside main()
int numFamily = hmap.get<2>().count( hhold2 );

int familyIDs[ numFamily ]; for ( int f = 0; f < numFamily; f++ ) { familyIDs[ f ] = 0; } int indID = 0; int f = 0; std::pair< HostsByHH::iterator, HostsByHH::iterator > pit = hmap.get<2>().equal_range( hhold2 ); cout << "\tNeed to update households of " << numFamily << " family members (including self) of host ID " << hid2 << endl; while ( pit.first != pit.second ) { cout << "Pointing at new family member still in hhold " << ((pit.first))->getHousehold() << "; " ; indID = ((pit.first) )->getID(); familyIDs[ f ] = indID; pit.first++; f++; }

What could make this code fail? The above snippet only runs when numFamily > 1. (Other suggestions and criticisms are welcome too.) Thank you in advance.

© Stack Overflow or respective owner

Related posts about c++

Related posts about boost