Problem with multi_index

Dear All, I am a newbie with multi_index container. I need to store the following data structure in the multi_index_container: ************************************* #if !defined(NDEBUG) #define BOOST_MULTI_INDEX_ENABLE_INVARIANT_CHECKING #define BOOST_MULTI_INDEX_ENABLE_SAFE_MODE #endif #include <boost/multi_index_container.hpp> #include <boost/multi_index/member.hpp> #include <boost/multi_index/ordered_index.hpp> #include <algorithm> #include <iostream> #include <iterator> #include <string> using boost::multi_index_container; using namespace boost::multi_index; struct base { int base_data; }; struct child : public base { int child_data; }; struct base_data{ }; typedef multi_index_container < child, indexed_by< ordered_non_unique< tag<base_data>, BOOST_MULTI_INDEX_MEMBER(child, int, base_data) > >
child_set;
int main() { child_set cs; return 0; } *************************************** If i try and compile the above code, I get the following error test.cpp:33: error: could not convert template argument &base::base_data to int child::* test.cpp:33: error: template argument 2 is invalid test.cpp:34: error: template argument 1 is invalid test.cpp:35: error: template argument 2 is invalid test.cpp:35: error: invalid type in declaration before ; token I have looked up the examples, but I can find any example for structs which are inherited. Can someone help? Regards, Sunil

Sunil Chomal ha escrito:
Dear All, I am a newbie with multi_index container. I need to store the following data structure in the multi_index_container:
[...]
struct base { int base_data; };
struct child : public base { int child_data; };
struct base_data{ };
typedef multi_index_container < child, indexed_by< ordered_non_unique< tag<base_data>, BOOST_MULTI_INDEX_MEMBER(child, int, base_data) >
child_set;
[...]
If i try and compile the above code, I get the following error test.cpp:33: error: could not convert template argument &base::base_data to int child::*
Hello Sunil, The problem is that, by a strange rule of C++, &base::base_data (which is the same as &child::base_data) is not statically recognized as a pointer to member of child. The first time this showed up I posted a request for clarification at comp.std.c++. with little success, see http://tinyurl.com/ykuu7w . You've got two options to solve the problem: 1. Write the key extractor as BOOST_MULTI_INDEX_MEMBER(base, int, base_data) (or as member<base,int,&base::base_data>). This will only work in Boost 1.34 (for Boost 1.33.1 and prior you'll get compile errors when trying to insert elements into the container), but if you can wait till Bost 1.34 is released or grab a CVS snapshot, this is my recommended option. 2. Write a custom key extractor: struct base_data_extractor { typedef int result_type; int operator()(const child &x)const{return x.base_data;} }; ... typedef multi_index_container < child, indexed_by< ordered_non_unique<tag<base_data>,base_data_extractor>
child_set;
HTH. Thank you for using Boost.MultiIndex, Joaquín M López Muñoz Telefónica, Investigación y Desarrollo
participants (2)
-
Joaquín Mª López Muñoz
-
Sunil Chomal