Click to See Complete Forum and Search --> : std::map problem


mwilliamson
January 19th, 2003, 11:13 AM
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.

DanM
January 19th, 2003, 11:47 AM
If you want to figure out an element with a specified key is present in the map, you should use map::find, not map::operator[]. map::operator[] 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/default.asp?url=/library/en-us/vcstdlib/html/vclrfMapfind.asp

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcstdlib/html/vclrfMapoperator[].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

mwilliamson
January 19th, 2003, 11:54 AM
more or less:


class MapClass
{
private:
std::map<std::string,std::string> m_map;
protected:
std::string GetValue(const std::string key) const
{
return m_map[key];
}
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!

DanM
January 19th, 2003, 12:15 PM
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

mwilliamson
January 19th, 2003, 12:20 PM
no, thats the exact code that generates the error.

I tried

std::string MapClass::GetValue(const std::string key) const
{
std::string value(m_map[key]);
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?

std::string MapClass::GetValue(const std::string key)
{
return m_map[key];
}

The above code compiles fine.

DanM
January 19th, 2003, 12:25 PM
Your method is const. But the map::operator[] is not const and can modify the m_varmap variable !
Now, either get rid of the const thing or make m_varmap mutable.

Dan

DanM
January 19th, 2003, 06:34 PM
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