[multi_index] passing 'const TYPE' as 'this' argument discards qualifiers

merry xmas everyone, and maybe santa claus or anyone of you have a helping surprise for me :o) after became acquainted with boost and boost.python i have to learn boost.multi_index now. the goal is to build a 'map' with two individual keys (a name and an identifier) per value (object of user class); nme1-idf1-valu1, nme2-idf2-valu2, nme3-idf3-valu3, ... any value should be 'directly' accessible by name or by identifier; valu1:=map[nme1], valu1:=map[idf1]. although i don't understand the documentation perfectly and i'm not familiar with templates i recognise that boost.multi_index is the perfect way :o) and i did the following: //--- cclass.hpp ----------- #ifndef _CCLASS_HPP_ #define _CCLASS_HPP_ class cClass { private: int att1; public: std::string nme; // name int idf; // identifier cClass(); cClass(std::string nme, int idf); int get_attribute(); }; typedef boost::multi_index_container < cClass, boost::multi_index::indexed_by < boost::multi_index::hashed_unique < BOOST_MULTI_INDEX_MEMBER(cMessage,std::string,nme) >, boost::multi_index::ordered_unique < BOOST_MULTI_INDEX_MEMBER(cMessage,int, idf) >
mMap;
typedef mMap::nth_index<0>::type mMapByNme; typedef mMap::nth_index<1>::type mMapByIdf; #endif //--- cclass.cpp ----------- #include <boost/multi_index_container.hpp> #include <boost/multi_index/member.hpp> #include <boost/multi_index/ordered_index.hpp> #include <boost/multi_index/hashed_index.hpp> #include "cclass.hpp" cClass::cClass() { this->att1=0xFF; } cClass::cClass(std::string nme, int idf) : nme(nme), idf(idf) { this->att1=idf+1; } int cClass::get_attribute() {return this->att1; } //--- main.cpp ------------- #include <boost/multi_index_container.hpp> #include <boost/multi_index/member.hpp> #include <boost/multi_index/ordered_index.hpp> #include <boost/multi_index/hashed_index.hpp> #include "cclass.hpp" int main() { mapMsg locMsg; locMsg.insert( cClass("name1", 123) ); locMsg.insert( cClass("name2", 456) ); mMapByNme::iterator itrNme = locMsg.get<0>().find("name1"); mMapByIdf::iterator itrIdf = locMsg.get<1>().find(456); printf( "att1 = %d\n", itrNme->get_attribute() ); // <<<<< printf statement } //-------------------------- compiling (mingw/gcc3.4.5) this code without the printf statement works. but if i compile the whole code i get this error message: main.cpp: In function `int main()': main.cpp:33: error: passing 'const cClass' as 'this' argument of 'int cClass::get_attribute()' discards qualifiers Process terminated with status 1 (0 minutes, 20 seconds) any hints how to solve the problem? how to encapsulate this special multi_index into a class, that fits the above description of the needed 'map'. are there possibilities to avoid the 'std::string' for the 'name' key? where are the more easier examples for C++-templates/boost newbies like me :o) ? many, many thnx, daniel _______________________________________________________________________ Jetzt neu! Schützen Sie Ihren PC mit McAfee und WEB.DE. 30 Tage kostenlos testen. http://www.pc-sicherheit.web.de/startseite/?mc=022220

On Dec 25, 2007 1:16 PM, <damny@web.de> wrote:
merry xmas everyone,
and maybe santa claus or anyone of you have a helping surprise for me :o)
after became acquainted with boost and boost.python i have to learn boost.multi_index now. the goal is to build a 'map' with two individual keys (a name and an identifier) per value (object of user class); nme1-idf1-valu1, nme2-idf2-valu2, nme3-idf3-valu3, ... any value should be 'directly' accessible by name or by identifier; valu1:=map[nme1], valu1:=map[idf1]. although i don't understand the documentation perfectly and i'm not familiar with templates i recognise that boost.multi_index is the perfect way :o) and i did the following:
<snip> Code And Problem </snip>
how to encapsulate this special multi_index into a class, that fits the above description of the needed 'map'. are there possibilities to avoid the 'std::string' for the 'name' key? where are the more easier examples for C++-templates/boost newbies like me :o) ?
You can definitely use Boost.MultiIndex to solve your problem. The thing is that a bidirectional map (a map where you can search in both ends) is a very common data structure, so a new library called Boost.Bimap was build on top of Boost.MultiIndex to give you a nicer interface that plays nicely with standard maps. In the most simple form you will use it like: #include <boost/bimap.hpp> using namespace boost::bimaps; typedef bimap<std::string, int> bm_type; int main() { bm_type locMsg; locMsg.insert( bm_type::value_type("name1", 123) ); locMsg.insert( bm_type::value_type("name2", 456) ); bm_type::left_iterator itrNme = locMsg.left.find("name1"); bm_type::right_iterator itrIdf = locMsg.right.find(456); // ... } This library will be included (If everything goes well) in the next release of Boost (1.35). For now, you can download it from Boost SVN Head. Online docs can be found here: http://tinyurl.com/22sja5 Best regards Matias
participants (2)
-
damny@web.de
-
Matias Capeletto