Having trouble printing out values from a map of maps

This is a discussion on Having trouble printing out values from a map of maps within the C++ Programming forums, part of the General Programming Boards category; Hi guys, as the title states, I'm having some trouble going through a map of maps and printing out all ...

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    19

    Having trouble printing out values from a map of maps

    Hi guys, as the title states, I'm having some trouble going through a map of maps and printing out all of the values. Here is my declaration of the map:
    Code:
    map<T, map<T, int> > adjList;
    And here is my Display function:
    Code:
    template<typename T>
    void Graph<T>::display() const
    {
    	map<T, map<T, int> >::iterator iter;
    	for (iter = adjList.begin(); iter != adjList.end(); ++iter)
    	{
    		cout << iter->first << endl;
    		map<T, int>::iterator pos;
    		for (pos = adjList[iter->first].begin(); pos != adjList[iter->first].end(); ++pos) 
    		{
    			cout << pos->first << "  " << pos->second << endl;
    		}
    	}
    }
    When I try to compile this, I get the follow error:
    Code:
    error C2679: binary '=' : no operator found which takes a right-hand operand of type 
    'std::_Tree_const_iterator<_Mytree>' (or there is no acceptable conversion)
    I'm pretty new to using iterators for this type of thing, and completely new to maps as well. Any help would be appreciated.

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,604
    I cannot reproduce your problem. Can you post a simple compilable example that demonstrates the problem?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    25
    How about
    Code:
    for (pos = iter->second.begin(); pos != iter->second.end(); ++pos)
    {
        cout << pos->first << "  " << pos->second << endl;
    }

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    Not 100% sure about this, but perhaps since you have a const member function and are using non-const iterators? The error message makes reference to const_iterator.
    error C2679: binary '=' : no operator found which takes a right-hand operand of type
    'std::_Tree_const_iterator<_Mytree>' (or there is no acceptable conversion)
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    Dec 2010
    Posts
    19
    Yes, the problem was using a non-const iterator in a const function. Thanks. Also used Caligulaminus code in place of mine; that is working too.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble Printing out Array
    By denZer in forum C Programming
    Replies: 6
    Last Post: 11-06-2011, 12:37 PM
  2. Printing values from Enum
    By Raskalnikov in forum C Programming
    Replies: 8
    Last Post: 02-16-2009, 09:28 PM
  3. Having trouble printing from an array
    By Sir Andus in forum C Programming
    Replies: 2
    Last Post: 10-30-2006, 12:48 PM
  4. trouble printing
    By dPmunky in forum C Programming
    Replies: 6
    Last Post: 11-18-2002, 01:43 PM
  5. printing values loop.
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 07-02-2002, 02:56 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21