How to use map containers whith odjects of user classes

Discussion in 'C++' started by Hatzigiannakis Nikos, Feb 20, 2008.

  1. I want to use a map container with objects of the following class
    rectangle. Each object will be accosiated with a key of type string:.

    #include <iostream>
    #include <string>
    #include <map>

    using namespace std;

    class rectangle
    {
    float side_a;
    float side_b;
    public:
    float area() {return side_a * side_b;}
    rectangle(float a, float b) {side_a=a; side_b=b;}
    };

    map<string,rectangle> mymap;

    main()
    {
    rectangle rec1(10,20);

    //the following statment does not compile
    mymap["table"] = rec1;
    system("pause");
    }

    1. The statment mymap["trapezi"] = rec1; supposed to add a new element in
    the map with the key "table" and value the object rec1 but it doesnt
    compile. why?

    2. If I finaly manage to add a new element how can I have access to the
    mebers of the rectangle object in a map element ?

    Thanks a lot
     
    Hatzigiannakis Nikos, Feb 20, 2008
    #1
    1. Advertising

  2. Hatzigiannakis Nikos

    Kai-Uwe Bux Guest

    Hatzigiannakis Nikos wrote:

    > I want to use a map container with objects of the following class
    > rectangle. Each object will be accosiated with a key of type string:.
    >
    > #include <iostream>
    > #include <string>
    > #include <map>
    >
    > using namespace std;
    >
    > class rectangle
    > {
    > float side_a;
    > float side_b;
    > public:
    > float area() {return side_a * side_b;}
    > rectangle(float a, float b) {side_a=a; side_b=b;}
    > };
    >
    > map<string,rectangle> mymap;
    >
    > main()
    > {
    > rectangle rec1(10,20);
    >
    > //the following statment does not compile
    > mymap["table"] = rec1;
    > system("pause");
    > }
    >
    > 1. The statment mymap["trapezi"] = rec1; supposed to add a new element in
    > the map with the key "table" and value the object rec1 but it doesnt
    > compile. why?


    The class rectangle has no default constructor.


    > 2. If I finaly manage to add a new element how can I have access to the
    > mebers of the rectangle object in a map element ?


    You could do:

    rectangle & rect = mymap["table"];
    // now use rect.


    Best

    Kai-Uwe Bux
     
    Kai-Uwe Bux, Feb 20, 2008
    #2
    1. Advertising

  3. Hatzigiannakis Nikos

    thomas Guest

    Re: How to use map containers whith odjects of user classes


    > map<string,rectangle> mymap;
    >
    > main()
    > {
    >     rectangle rec1(10,20);
    >
    >     //the following statment does not compile
    >     mymap["table"] = rec1;
    >     system("pause");
    >
    > }
    >

    mymap["table"] cannot be a left value.
    mybe you need to redefine mymap:

    map<string,rectangle&> mymap;
     
    thomas, Feb 20, 2008
    #3
  4. Hatzigiannakis Nikos

    Kai-Uwe Bux Guest

    Re: How to use map containers whith odjects of user classes

    thomas wrote:

    >
    >> map<string,rectangle> mymap;
    >>
    >> main()
    >> {
    >> rectangle rec1(10,20);
    >>
    >> //the following statment does not compile
    >> mymap["table"] = rec1;
    >> system("pause");
    >>
    >> }
    >>

    > mymap["table"] cannot be a left value.


    What makes you think that? The return type of non-const operator[] in a
    std::map is mapped_type&. The operator[] is overloaded precisely so that
    you can assign into the map. However, it required the mapped_type to be
    default constructible.


    > mybe you need to redefine mymap:
    >
    > map<string,rectangle&> mymap;


    You did not run that by a compiler, did you? If you did and if the compiler
    did not choke, may I suggest you ditch that compiler.


    Best

    Kai-Uwe Bux
     
    Kai-Uwe Bux, Feb 20, 2008
    #4
  5. Hatzigiannakis Nikos

    James Kanze Guest

    Re: How to use map containers whith odjects of user classes

    On Feb 20, 8:09 am, "Hatzigiannakis Nikos" <> wrote:
    > I want to use a map container with objects of the following
    > class rectangle. Each object will be accosiated with a key of
    > type string:.


    > #include <iostream>
    > #include <string>
    > #include <map>


    > using namespace std;


    > class rectangle
    > {
    > float side_a;
    > float side_b;
    > public:
    > float area() {return side_a * side_b;}
    > rectangle(float a, float b) {side_a=a; side_b=b;}
    > };


    > map<string,rectangle> mymap;


    > main()
    > {
    > rectangle rec1(10,20);


    > //the following statment does not compile
    > mymap["table"] = rec1;
    > system("pause");
    > }


    > 1. The statment mymap["trapezi"] = rec1; supposed to add a new element in
    > the map with the key "table" and value the object rec1 but it doesnt
    > compile. why?


    The sub-expression mymap["trapezi"] returns a reference to the
    object. If the object doesn't exist, it creates it, using the
    object type's default constructor. Since rectangle doesn't have
    a default constructor, it fails. (Note that any use of [] will
    fail, even if not being used for insertion.)

    The "standard" way to insert an object into a map is with
    map<>::insert. The "standard" way to read an object present in
    the map is with map<>::find. The [] in map is a convenience
    function, for certain specific uses of map, and certain uses
    only. (There is no one semantic that it could be given which
    would be appropriate for all uses.)

    You can either provide rectangle with a default constructor
    (probably by specifying default arguments to the present
    constructor), or use the "standard" functions insert and find to
    access the map. Which solution is best depends on your
    application.

    > 2. If I finaly manage to add a new element how can I have
    > access to the mebers of the rectangle object in a map element?


    See above. Either find(), or operator[]. Just be aware that
    operator[] will insert an entry if one is not already present,
    which may not be what you want.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Feb 20, 2008
    #5
  6. Re: How to use map containers whith odjects of user classes

    Ok I put some default values in the constructor and worked fine.

    I tried to use the insert method

    mymap,insert("new",rectangle(12,12))

    but it didnt work. Do I have any mistake?
     
    Hatzigiannakis Nikos, Feb 20, 2008
    #6
  7. Hatzigiannakis Nikos

    Guest

    Re: How to use map containers whith odjects of user classes

    On Feb 20, 3:04 pm, "Hatzigiannakis Nikos" <> wrote:
    > Ok I put some default values in the constructor and worked fine.
    >
    > I tried to use the insert method
    >
    > mymap,insert("new",rectangle(12,12))
    >
    > but it didnt work. Do I have any mistake?


    map stores values as pair. you can use std::pair or std::make_pair

    mymap.insert(std::pair<std::string,rectangle>("new",rectangle(12,12)));

    Thanks,
    Balaji.
     
    , Feb 20, 2008
    #7
  8. Hatzigiannakis Nikos

    James Kanze Guest

    Re: How to use map containers whith odjects of user classes

    On Feb 20, 11:04 am, "Hatzigiannakis Nikos" <> wrote:
    > Ok I put some default values in the constructor and worked fine.


    > I tried to use the insert method


    > mymap,insert("new",rectangle(12,12))


    > but it didnt work. Do I have any mistake?


    Yes. You didn't read the documentation first. (I like to be
    helpful, but not even getting the number of arguments right
    can't be a problem of understanding.)

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Feb 20, 2008
    #8
  9. Hatzigiannakis Nikos

    Martin York Guest

    Re: How to use map containers whith odjects of user classes

    On Feb 20, 3:39 am, wrote:
    > On Feb 20, 3:04 pm, "Hatzigiannakis Nikos" <> wrote:
    >
    > > Ok I put some default values in the constructor and worked fine.

    >
    > > I tried to use the insert method

    >
    > > mymap,insert("new",rectangle(12,12))

    >
    > > but it didnt work. Do I have any mistake?

    >
    > map stores values as pair. you can use std::pair or std::make_pair
    >
    > mymap.insert(std::pair<std::string,rectangle>("new",rectangle(12,12)));
    >
    > Thanks,
    > Balaji.


    Though this is perfectly valid. Personally, I prefer to use
    make_pair() as this automatically gets the correct types from the
    parameters we use.

    mymap.insert(std::make_pair(std::string("new"),rectangle(12,12)));
     
    Martin York, Feb 21, 2008
    #9
  10. Hatzigiannakis Nikos

    James Kanze Guest

    Re: How to use map containers whith odjects of user classes

    On Feb 21, 4:25 am, Martin York <> wrote:
    > On Feb 20, 3:39 am, wrote:
    > > On Feb 20, 3:04 pm, "Hatzigiannakis Nikos" <> wrote:


    > > > Ok I put some default values in the constructor and worked fine.


    > > > I tried to use the insert method


    > > > mymap,insert("new",rectangle(12,12))


    > > > but it didnt work. Do I have any mistake?


    > > map stores values as pair. you can use std::pair or std::make_pair


    > > mymap.insert(std::pair<std::string,rectangle>("new",rectangle(12,12)));

    >
    > Though this is perfectly valid. Personally, I prefer to use
    > make_pair() as this automatically gets the correct types from the
    > parameters we use.


    > mymap.insert(std::make_pair(std::string("new"),rectangle(12,12)));


    Actually, it doesn't. It does get a pair which will convert to
    the correct type, if the arguments are acceptable types, and if
    the library is up to date, but the correct type is
    MapType::value_type. If you have a typedef for the map:

    mymap.insert( Map::value_type( "new", rectangle(12,12) ) ) ;

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Feb 21, 2008
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Linus Sylv?n

    virtual keyboard whith Robot.keyPress

    Linus Sylv?n, Aug 16, 2004, in forum: Java
    Replies:
    3
    Views:
    13,410
    hawkansson
    Jun 3, 2009
  2. bonnet
    Replies:
    0
    Views:
    561
    bonnet
    Apr 8, 2005
  3. edw
    Replies:
    2
    Views:
    9,534
  4. Replies:
    7
    Views:
    561
    Pete Becker
    Jan 25, 2008
  5. Sebastian Mach
    Replies:
    5
    Views:
    328
Loading...

Share This Page