Question on boost multi-index container erase method

Hello Joaquin, I am trying to erase a record in the container based on keys - in my case both the keys are ordered and unique and I need to have ways to delete records based on either of the keys. I tried to erase an element based on key2 & I got the following error: "error: no matching function for call to 'boost::multi_index ....<Error runs for more than a page> :erase(boost::multi_index::detail::index_iterator <boost::multi_index::detail::ordered_index_node<boost::multi_index::detail ::index_node_base<MyMap> > >&)' The compiler is perhaps complaining that I havent provided a prototype for erase in the map? In my case I wish to erase the record pointed by the iterator (In another class I had a single index and I didnt have the need to provide a prototype, is it because I have two 'unsigned long' keys that the compiler expects a prototype? )- Kindly help me fix this error. My code is posted as below: Thanks /R --- //fwd decl class A; struct MyMap { unsigned long key1; unsigned long key2; A *pObj; MyMap(unsigned long param_key1, unsigned long param_key2, A *param_pObj): key1(param_key1), key2(param_key2), pObj(param_pObj){} ~LCMap(){ key1 = 0; key2 = 0; pObj = 0; } }; struct key1{}; struct key2{}; typedef multi_index_container< MyMap, indexed_by< ordered_unique< tag <key1>, BOOST_MULTI_INDEX_MEMBER(MyMap, unsigned long, key1)>, ordered_unique< tag <key2>, BOOST_MULTI_INDEX_MEMBER(MyMap, unsigned long, key2)> >
tMapDef;
class MyWrap { private: MyMap MyContainer; .. public: insert(UINT32, UINT32, A *); erasebykey2(unsigned long); }; bool MyWrap::erasebykey2(unsigned long param_key2) { bool ret = FALSE; typedef tMapDef::index<key2>::type tKey2Type; tKey2Type& key2_index = MyContainer.get<key2>(); tMapDef::index_iterator<key2>::type it = key2_index.find(param_key2); if (it == key2_index.end()) { cout << "No matching Recs with key2" <<endl<<; return FALSE; } tMapDef::size_type status = MyContainer.erase(it); }

Ramesh escribió:
Hello Joaquin,
I am trying to erase a record in the container based on keys - in my case both the keys are ordered and unique and I need to have ways to delete records based on either of the keys.
I tried to erase an element based on key2 & I got the following error:
"error: no matching function for call to 'boost::multi_index ....<Error runs for more than a page>
:erase(boost::multi_index::detail::index_iterator <boost::multi_index::detail::ordered_index_node<boost::multi_index::detail ::index_node_base<MyMap> > >&)'
[...] tMapDef::index_iterator<key2>::type it = key2_index.find(param_key2); [...] tMapDef::size_type status = MyContainer.erase(it);
You're retrieving a key2-index iterator and trying to use it on key1-index. Replace your last line with tMapDef::size_type status = key2_index.erase(it); OK now? Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

Apologies for a late response, i was on travel. Thank you very much. Works perfectly. I checked the prototype for erase - it returns a count of objects that it erases, in my case - after I made the code change as you suggested, compiler flagged an error saying couldnt convert iterator to size_t type. error: cannot convert 'boost::multi_index::detail::index_iterator<boost::multi_index::detail::ordered_index_node<boost::multi_index::detail::index_node_base<<MyMap>>
' to 'size_t' in initialization
Is the datatype of status wrong? Thanks Ramesh On Tue, Sep 30, 2008 at 10:56 PM, <joaquin@tid.es> wrote:
Ramesh escribió:
Hello Joaquin,
I am trying to erase a record in the container based on keys - in my case both the keys are ordered and unique and I need to have ways to delete records based on either of the keys.
I tried to erase an element based on key2 & I got the following error:
"error: no matching function for call to 'boost::multi_index ....<Error runs for more than a page>
:erase(boost::multi_index::detail::index_iterator <boost::multi_index::detail::ordered_index_node<boost::multi_index::detail ::index_node_base<MyMap> > >&)'
[...] tMapDef::index_iterator<key2>::type it = key2_index.find(param_key2); [...] tMapDef::size_type status = MyContainer.erase(it);
You're retrieving a key2-index iterator and trying to use it on key1-index. Replace your last line with
tMapDef::size_type status = key2_index.erase(it);
OK now?
Joaquín M López Muñoz Telefónica, Investigación y Desarrollo _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

Ramesh escribió:
Apologies for a late response, i was on travel.
Thank you very much. Works perfectly. I checked the prototype for erase - it returns a count of objects that it erases, in my case - after I made the code change as you suggested, compiler flagged an error saying couldnt convert iterator to size_t type.
error: cannot convert 'boost::multi_index::detail::index_iterator<boost::multi_index::detail::ordered_index_node<boost::multi_index::detail::index_node_base<<MyMap>>
' to 'size_t' in initialization
Is the datatype of status wrong?
Oh, yes, it is wrong, I overlooked that on my previous post. erase(iterator) does not return a count of erased elements but an iterator to the element following the one that was erased. It is erase(const key_type&) that returns a count. This is documented at http://tinyurl.com/3k728t and mimics the interface of STL associative containers. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo
participants (2)
-
joaquin@tid.es
-
Ramesh