Boost logo

Boost Users :

From: damny_at_[hidden]
Date: 2007-12-25 11:16:11


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


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net