f



How to implement a map of maps?

Hi!

I am trying to implement a map of maps like this:

typedef map<string, map<string, string> > stringMap;
stringMap myMap;

This does not work and I get the following compiler error:

---

/usr/lib/gcc/i386-redhat-linux/3.4.6/../../../../include/c++/3.4.6/
bits/stl_pair.h: In constructor `std::pair<_T1, _T2>::pair(const
std::pair<_U1, _U2>&) [with _U1 = char*, _U2 = char*, _T1 = const
std::string, _T2 = std::map<std::string, std::string,
std::less<std::string>, std::allocator<std::pair<const std::string,
std::string> > >]':
src/png.cpp:182:   instantiated from here
/usr/lib/gcc/i386-redhat-linux/3.4.6/../../../../include/c++/3.4.6/
bits/stl_pair.h:90: error: no matching function for call to
`std::map<std::string, std::string, std::less<std::string>,
std::allocator<std::pair<const std::string, std::string> > >::map
(char* const&)'
/usr/lib/gcc/i386-redhat-linux/3.4.6/../../../../include/c++/3.4.6/
bits/stl_map.h:166: note: candidates are: std::map<_Key, _Tp,
_Compare, _Alloc>::map(const std::map<_Key, _Tp, _Compare, _Alloc>&)
[with _Key = std::string, _Tp = std::string, _Compare =
std::less<std::string>, _Alloc = std::allocator<std::pair<const
std::string, std::string> >]
/usr/lib/gcc/i386-redhat-linux/3.4.6/../../../../include/c++/3.4.6/
bits/stl_map.h:156: note:                 std::map<_Key, _Tp,
_Compare, _Alloc>::map(const _Compare&, const typename
std::_Rb_tree<_Key, std::pair<const _Key, _Tp>,
std::_Select1st<std::pair<const _Key, _Tp> >, _Compare,
_Alloc>::allocator_type&) [with _Key = std::string, _Tp = std::string,
_Compare = std::less<std::string>, _Alloc =
std::allocator<std::pair<const std::string, std::string> >]
/usr/lib/gcc/i386-redhat-linux/3.4.6/../../../../include/c++/3.4.6/
bits/stl_map.h:148: note:                 std::map<_Key, _Tp,
_Compare, _Alloc>::map() [with _Key = std::string, _Tp = std::string,
_Compare = std::less<std::string>, _Alloc =
std::allocator<std::pair<const std::string, std::string> >]
gmake: *** [bmdb2ken] Error 1

---

I have been searching the internet for hours and still not found a
solution.
I am pretty new to C++, so could someone please give me a hint?

Thanks a lot!
Regards,
Andrea

-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Andrea
1/15/2009 8:54:17 AM
comp.lang.c++.moderated 10738 articles. 1 followers. allnor (8509) is leader. Post Follow

3 Replies
880 Views

Similar Articles

[PageSpeed] 21

On Jan 15, 9:54 am, Andrea Scharfe <schm...@gmail.com> wrote:

> I am trying to implement a map of maps like this:
>
> typedef map<string, map<string, string> > stringMap;
> stringMap myMap;
>
> This does not work and I get the following compiler error:

The fallowing full program compiles and works as expected:
#include <iostream>
#include <string>
#include <map>

using namespace std;
int main()
{
    typedef map<string, map<string, string> > stringMap;
    stringMap myMap;
    myMap["123"]["223"] = "Harg";

    cout << myMap["123"]["223"] << '\n';

    return 0;
}

Obviously, there's something wrong, other than your creation of a map
of maps. When you post code here, please post a full program that
reproduces your problem. We can then tell you if there's a problem
with how you're iterating or using your map. If perfectly working code
doesn't work for you and only you, we can even tell you if there's a
bug in your compiler! (Not that it's very common.)


-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Hakusa
1/15/2009 10:31:02 AM
Andrea Scharfe wrote:

> I am trying to implement a map of maps like this:
> 
> typedef map<string, map<string, string> > stringMap;
> stringMap myMap;
> 
> This does not work and I get the following compiler error:
> 
> ---
> 
> /usr/lib/gcc/i386-redhat-linux/3.4.6/../../../../include/c++/3.4.6/
> bits/stl_pair.h: In constructor `[snip]':
> src/png.cpp:182:   instantiated from here
> [snip]error: no matching function for call to
> `std::map<std::string, std::string, std::less<std::string>,
> std::allocator<std::pair<const std::string, std::string> > >::map
> (char* const&)'
[snip]
> I have been searching the internet for hours and still not found a
> solution.
> I am pretty new to C++, so could someone please give me a hint?

I don't know what happen at line 182 of png.cpp but from the error 
message, you are trying to construct a stringMap with a char*const 
parameter which indeed doesn't exists.

Do you have something like ?
stringMap smap("my map");

-- 
Michael

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Michael
1/16/2009 1:52:25 PM
On Jan 15, 3:54 pm, Andrea Scharfe <schm...@gmail.com> wrote:
>
> error: no matching function for call to
> `std::map<std::string, std::string>::map(char* const&)'

Hi,

This is what your error message looks like when all the unnecessary
cruft is removed. So you're trying to construct a map<string, string>
from a const char*, which suggests that you're doing something like

myMap.insert(std::make_pair("key", "value"));

Sebastian


-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
wasti
1/18/2009 4:53:11 AM
Reply: