CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Dec 2001
    Location
    Ontario, Canada
    Posts
    2,236

    std::map problem

    Sorry if I'm getting anoying, I can't std::map to work right.
    Heres my code:

    std::map<std::string,std::string> m_map;
    ...
    m_map[key] = value;
    ...
    std::string value(m_map[key]);

    The last line generates the error:
    error C2678: binary '[' : no operator defined which takes a left-hand operand of type 'const class std::map<class std::basic_string<char,struct std::char_traits<ch...

    From what I remember, and see on the internet this is how map is supposed to work.. so why doesn't it? I have tried everything (except looping through ) to get my values back from map.

  2. #2
    Join Date
    Dec 2002
    Posts
    287
    If you want to figure out an element with a specified key is present in the map, you should use map::find, not map:perator[]. map:perator[] has a small problem:

    If the argument key value is not found, then it is inserted along with the default value of the data type.

    http://msdn.microsoft.com/library/de...lrfMapfind.asp

    http://msdn.microsoft.com/library/de...lrfMapoperator[].asp


    Anyway, coming back to your compiling problem: can you post the exact C++ code and especially the value & key variable declaration ? I don't believe all the lines are in the same method because you use value in thes second line and then you declare it again in the last line.

    Dan

  3. #3
    Join Date
    Dec 2001
    Location
    Ontario, Canada
    Posts
    2,236
    more or less:

    Code:
    class MapClass
    {
    private:
    std::map<std::string,std::string> m_map;
    protected:
    std::string GetValue(const std::string key) const
    {
    return m_map&#091;key&#093;;
    }
    void SetValue(const std::string key.const std::string value)
    {
    m_map[key] = value;
    }
    }
    I know that the operator will create a new element if none exists, but the way my program is working there will be more sets than gets. And if it tries to get a key that doesn't exist, I am ok with returning "".

    Thanks for your help!
    Last edited by mwilliamson; January 19th, 2003 at 12:58 PM.

  4. #4
    Join Date
    Dec 2002
    Posts
    287
    And where is the last line:

    std::string value(m_map[key]);

    ?

    Most probaly you are passing a map const reference which is not OK when using it with the [] operator. Look at the error message:

    const class std::map

    Dan

  5. #5
    Join Date
    Dec 2001
    Location
    Ontario, Canada
    Posts
    2,236
    no, thats the exact code that generates the error.

    I tried

    Code:
    std::string MapClass::GetValue(const std::string key) const
    {
      std::string value(m_map&#091;key&#093;);
      return value;
    }
    To get rid of the error, but both are the same.

    From your suggestion, I removed the const return value from GetValue, and the error is gone. Why though?

    Code:
    std::string MapClass::GetValue(const std::string key)
    {
      return m_map&#091;key&#093;;
    }
    The above code compiles fine.
    Last edited by mwilliamson; January 19th, 2003 at 01:25 PM.

  6. #6
    Join Date
    Dec 2002
    Posts
    287
    Your method is const. But the map:perator[] is not const and can modify the m_varmap variable !
    Now, either get rid of the const thing or make m_varmap mutable.

    Dan

  7. #7
    Join Date
    Dec 2002
    Posts
    287
    Just compiled your code with the following change:

    1) Making the MapClass::GetValue non const

    or

    2) Keeping MapClass::GetValue const and making m_varmap mutable

    and it compiles without any errors

    Dan

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured