Map of maps


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Map of maps

  1. #1
    Join Date
    May 2004
    Posts
    242

    Map of maps

    Hi all,

    I've got :-
    Code:
    #include <iostream>
    #include <string>
    #include <map>
    
    using namespace std;
    
    template <typename K, typename V, class C, class A>
    ostream &operator<< (ostream &os, map<K,V,C,A> const& m)
    {
            os << "{ ";
            typename map<K,V,C,A>::const_iterator p;
            for (p = m.begin(); p != m.end(); ++p) 
            {
                    os << p->first << ":" << p->second << ", ";
            }
            return os << "}";
    }
    
    int main (int argc, char *argv[])
    {
            typedef map<int, string> Map1;
            typedef map<double, Map1> Map2;
    
            Map2 m;
            m.insert (make_pair (1.2, Map1 ()));
            m[1.2].insert (make_pair (3, "three"));
            m[1.2].insert (make_pair (5, "five"));
    
            m.insert (make_pair (.5, Map1 ()));
            m[.5].insert (make_pair (5, "five"));
            m[.5].insert (make_pair (1, "one"));
            m[.5].insert (make_pair (9, "nine"));
    
            std::cout << m << endl;
    }
    But I'm getting this compilation error :-
    Quote Originally Posted by Error
    "/hfx/opt/Sun_ONE_Studio_11_Compiler_Collection/SUNWspro/prod/include/CC/Cstd/./utility", line 101: Error: Cannot initialize array member "second" in a constructor argument list.
    "mapofmaps.cpp", line 26: Where: While instantiating "std::pair<int, char[6]>::pair(const int&, const char(&)[6])".
    "mapofmaps.cpp", line 26: Where: Instantiated from non-template code.
    "mapofmaps.cpp", line 26: Error: Could not find a match for std::map<int,std::string>::insert(std::pair<int, char[6]>) needed in main(int, char**).
    "mapofmaps.cpp", line 27: Error: Could not find a match for std::map<int,std::string>::insert(std::pair<int, char[5]>) needed in main(int, char**).
    "mapofmaps.cpp", line 30: Error: Could not find a match for std::map<int,std::string>::insert(std::pair<int, char[5]>) needed in main(int, char**).
    "mapofmaps.cpp", line 31: Error: Could not find a match for std::map<int,std::string>::insert(std::pair<int, char[4]>) needed in main(int, char**).
    "mapofmaps.cpp", line 32: Error: Could not find a match for std::map<int,std::string>::insert(std::pair<int, char[5]>) needed in main(int, char**).
    Can someone please help with this ?

    I tried using pair instead of make_pair but this didn't matter

    Many thanks,

    Imanuel.
    Last edited by Hack; 09-03-2009 at 03:53 PM. Reason: Added Code Tags

  2. #2
    Join Date
    Aug 2009
    Posts
    13
    It seems to be a minor compiler problem, so I think all you need is to replace
    Code:
    insert (make_pair (3, "three"));
    to
    Code:
    insert (make_pair (3, string("three")));
    everywhere.

  3. #3
    Join Date
    May 2004
    Posts
    242
    Thanks for that.

    I managed to get this to work.


    Many thanks.
    Last edited by ami; 09-06-2009 at 12:51 AM.

Similar Threads

  1. Swapping a map element (a DOM question)
    By alsoares in forum AJAX
    Replies: 2
    Last Post: 12-20-2007, 08:03 PM
  2. VC++ map
    By GOBLIN-85 in forum C++
    Replies: 2
    Last Post: 11-16-2007, 11:34 AM
  3. map and memory leak
    By WXY595 in forum C++
    Replies: 3
    Last Post: 05-16-2007, 04:06 PM
  4. Parse string with delimiters
    By zion in forum VB Classic
    Replies: 2
    Last Post: 02-27-2006, 03:27 PM
  5. map areas not passing Form object to java script
    By mark hembree in forum ASP.NET
    Replies: 1
    Last Post: 01-11-2001, 11:21 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center