Note that there are some explanatory texts on larger screens.

plurals
  1. POIterator failure while moving over equal_range in Boost MultiIndex container
    text
    copied!<p>I'm making some mistake with my iterators, but I can't see it yet.</p> <p>I have a Boost MultiIndex container, <code>HostContainer hmap</code>, whose elements are <code>boost::shared_ptr</code> to members of class <code>Host</code>. All the indices work on member functions of class Host. The third index is by <code>Host::getHousehold()</code>, where the <code>household</code> member variable is an <code>int</code>. </p> <p>Below, I'm trying to iterate over the range of Hosts matching a particular household (<code>int hhold2</code>) and load the corresponding private member variable <code>Host::id</code> 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.)</p> <pre><code>typedef multi_index_container&lt; boost::shared_ptr&lt; Host &gt;, indexed_by&lt; hashed_unique&lt; const_mem_fun&lt;Host,int,&amp;Host::getID&gt; &gt;, // 0 - ID index ordered_non_unique&lt; const_mem_fun&lt;Host,int,&amp;Host::getAgeInY&gt; &gt;, // 1 - Age index ordered_non_unique&lt; const_mem_fun&lt;Host,int,&amp;Host::getHousehold&gt; &gt; // 2 - Household index &gt; // end indexed_by &gt; HostContainer; typedef HostContainer::nth_index&lt;2&gt;::type HostsByHH; // inside main() int numFamily = hmap.get&lt;2&gt;().count( hhold2 ); int familyIDs[ numFamily ]; for ( int f = 0; f &lt; numFamily; f++ ) { familyIDs[ f ] = 0; } int indID = 0; int f = 0; std::pair&lt; HostsByHH::iterator, HostsByHH::iterator &gt; pit = hmap.get&lt;2&gt;().equal_range( hhold2 ); cout &lt;&lt; "\tNeed to update households of " &lt;&lt; numFamily &lt;&lt; " family members (including self) of host ID " &lt;&lt; hid2 &lt;&lt; endl; while ( pit.first != pit.second ) { cout &lt;&lt; "Pointing at new family member still in hhold " &lt;&lt; (*(pit.first))-&gt;getHousehold() &lt;&lt; "; " ; indID = (*(pit.first) )-&gt;getID(); familyIDs[ f ] = indID; pit.first++; f++; } </code></pre> <p>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.</p> <hr> <p>I replaced the while() loop with</p> <pre><code>for ( HostsByHH::iterator fit = pit.first; fit != pit.second; fit++ ) { cout &lt;&lt; "Pointing at new family member still in hhold " &lt;&lt; (*fit)-&gt;getHousehold() &lt;&lt; "; " ; indID = (*fit )-&gt;getID(); cout &lt;&lt; "id=" &lt;&lt; indID &lt;&lt; endl; familyIDs[ f ] = indID; f++; } </code></pre> <p>This seems to work.</p> <p>I don't quite understand how this is a fix or why the earlier error only occurred with households of size 2. Any insight would be very welcome.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload