I am running into a compiler issue when using map class, and wrote the following simple program to highlight the error:
1 #include <string>
2 #include <map>
3
4 using namespace std;
5
6 int main()
7 {
8 map<string, string> testmap;
9
10
11 testmap["one"] = 11;
12 testmap["two"] = 22;
13 testmap["zero"] = 0;
14 // testmap["zero"] = 10;
15
16 return 0;
17 }
I get the following compilation error:
g++ ./test.cc ./test.cc: In function 'int main()': ./test.cc:13:23: error: ambiguous overload for 'operator=' in 'testmap.std::map<_Key, _Tp, _Compare, _Alloc>::operator[], std::basic_string, std::less >, >std::allocator, std::basic_string > > >((* & std::basic_string(((const char*)"zero"), ((const std::allocator)(& std::allocator()))))) = 0' ./test.cc:13:23: note: candidates are: In file included from /usr/include/c++/4.7/string:54:0, from ./test.cc:1: /usr/include/c++/4.7/bits/basic_string.h:543:7: note: std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::operator=(const std::basic_string<_CharT, _Traits, _Alloc>&) [with >_CharT = char; _Traits = std::char_traits; _Alloc = std::allocator; std::basic_string<_CharT, _Traits, _Alloc> = std::basic_string] /usr/include/c++/4.7/bits/basic_string.h:551:7: note: std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::operator=(const _CharT*) [with _CharT = char; _Traits = >std::char_traits; _Alloc = std::allocator; std::basic_string<_CharT, _Traits, _Alloc> = std::basic_string] /usr/include/c++/4.7/bits/basic_string.h:562:7: note: std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::operator=(_CharT) [with _CharT = char; _Traits = std::char_traits; _Alloc = std::allocator; std::basic_string<_CharT, _Traits, _Alloc> = std::basic_string]
I have a few questions: 1. Initially I thought the map "values" - 11 and 22 were being converted to string. However, after getting this compiler error makes me believe otherwise. What is exactly happening under the hood in the following: testmap["one"] = 11;
If the values 11, 22 is in fact converted to string, then why is 0 not converted to a string.
I am trying hard to understand the compiler error message, so I can decode the error myself. I understand parts of it, where the template map class definition is expanded with the key/value being of type string. Can someone please point me to the parts of the error message that may help me understand the issue.
Any help is much appreciated.
Thank you, Ahmed.