Thread: std::map probs

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    20

    std::map probs

    Hi,

    i got following code:

    Code:
    typedef std::map<int, LPITEMIDLIST> IdIDListMap; 
    IdIDListMap map;
    so far so good everything works expect that one:
    Code:
    int index = "SomeNumber";
    LPITEMIDLIST lPIDL = map[index];
    my compiler says:
    Error 1 error C2678: binary '[' : no operator found which takes a left-hand operand of type 'const IdIDListMap' (or there is no acceptable conversion)

    as far as i know the operator[] is declared that way:
    TYPE& operator[]( key_type& key )
    so what am i doing wrong.
    Sorry if its horribly stupid or already answered, but i tried searching for sth similiar and gave up after two pages of wrong posts
    Last edited by BeBu; 08-05-2007 at 03:50 PM. Reason: typos

  2. #2
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    Code:
    int index = "SomeNumber";
    This is an error, you can't assign a string literal to an int.


    For your map, I wouldn't use the [ ] operator, instead, prefer to use find(), which is a nonmodifying function.

    Even if your map isn't declared const, I assume its declared as a member of a class (although perhaps you'd like to confirm where your 'map' comes from), and my guess is that you're probably trying to call this code from a const member function, which isn't allowed to call modifying functions of the map.

    try this instead - (untested)
    Code:
    IdIDListMap::const_iterator iter = map.find(index);
    LPITEMIDLIST lPIDL;
    if( iter )
        lPIDL = iter->second;
    Last edited by Bench82; 08-05-2007 at 04:48 PM.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    When you use operator[], it automatically creates a new entry in the map if one doesn't exist. Sometimes that is what you want, in which case you have to make sure you aren't in a const member function or using a const map as Bench82 said.

    If you don't want to create an element if none exists, then use find. The example for find should test it against end(), though, not null:
    Code:
    IdIDListMap::const_iterator iter = map.find(index);
    LPITEMIDLIST lPIDL;
    if( iter != map.end() )
        lPIDL = iter->second;

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    Thanks for that correction, Daved, I guess that's what they call a brain-fart.

  5. #5
    Registered User
    Join Date
    Dec 2004
    Posts
    20
    Code:
    int index = "SomeNumber";
    was just an to show i get some number

    //well i already figured out how to use find, but i would love to know why the compiler thinks it is illegal access.
    // But thanks for the help.
    Nevermind thanks Bench82 its exaclty what you say !
    Last edited by BeBu; 08-06-2007 at 11:51 AM. Reason: am stupid :D

  6. #6
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    Quote Originally Posted by BeBu View Post
    Code:
    int index = "SomeNumber";
    was just an to show i get some number

    well i already figured out how to use find, but i would love to know why the compiler thinks it is illegal access.
    But thanks for the help.
    somewhere you have made a 'const' promise to your compiler, and the [ ] operator breaks that promise, so the compiler complains. However, without seeing more of your code, there's no way to pinpoint where you've made that promise. (The promise doesn't have to be at the declaration of the map)

  7. #7
    Registered User
    Join Date
    Dec 2004
    Posts
    20
    yes thanks
    i figured it out:

    its in a const class function.
    dude i owe you one. next tiem you are in germany come and i sepnt you a good beer

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. store in std::map all kind of structs
    By umen242 in forum C++ Programming
    Replies: 7
    Last Post: 10-19-2008, 02:24 AM
  2. std::map Alternatives
    By EvilGuru in forum C++ Programming
    Replies: 8
    Last Post: 04-12-2008, 03:45 AM
  3. Is std::map efficient for this problem?
    By dudeomanodude in forum C++ Programming
    Replies: 12
    Last Post: 04-10-2008, 02:15 PM
  4. std::map question.
    By Raigne in forum C++ Programming
    Replies: 7
    Last Post: 03-19-2008, 12:29 PM
  5. passing std::map from one dll to another
    By Carlos in forum Windows Programming
    Replies: 2
    Last Post: 05-16-2003, 07:45 AM