|
-
January 19th, 2003, 12:13 PM
#1
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.
-
January 19th, 2003, 12:47 PM
#2
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
-
January 19th, 2003, 12:54 PM
#3
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[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!
Last edited by mwilliamson; January 19th, 2003 at 12:58 PM.
-
January 19th, 2003, 01:15 PM
#4
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
-
January 19th, 2003, 01:20 PM
#5
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[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?
Code:
std::string MapClass::GetValue(const std::string key)
{
return m_map[key];
}
The above code compiles fine.
Last edited by mwilliamson; January 19th, 2003 at 01:25 PM.
-
January 19th, 2003, 01:25 PM
#6
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
-
January 19th, 2003, 07:34 PM
#7
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|