Thread: problem with a map iterator

  1. #16
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Hi Daved,

    Thanks for taking the time to look over my code. I previously tried most of the things you posted, but I couldn't get any of them to work. Here are the errors:
    mapfill->first: The Lit object (key)
    Code:
    map<Lit, vector<Index> >::const_iterator mapfill;
    for(mapfill = M.begin(); mapfill != M.end(); mapfill++)
    {
    	mapfill->first.display();  //ERROR
    }
    error C2662: 'display' : cannot convert 'this' pointer from 'const class Lit' to 'class Lit &' Conversion loses qualifiers

    mapfill->second: The vector<Index> (value)
    Code:
    map<Lit, vector<Index> >::const_iterator mapfill;
    for(mapfill = M.begin(); mapfill != M.end(); mapfill++)
    {
    	(mapfill->second)[0].show(); //ERROR
    }
    error C2662: 'show' : cannot convert 'this' pointer from 'const class Index' to 'class Index &'
    Conversion loses qualifiers


    I did get this one to work:

    M[v][1].show();

    If you are using iterators, then you don't need the operator[]:
    Yes. I realized when I was testing my code that using an iterator obviated the need to do a key look up: I could just use iterator->second[x]. But, I wanted to test the key look up process anyway, so I tried using the iterator to get the Lit object key, but I couldn't get it to work. The syntax M[*mapfill][0].show()....hmmm, just had an idea. Yep, this works:

    M[mapfill->first][0].show();

    I forgot mapfill points to a pair type.
    Last edited by 7stud; 04-30-2005 at 03:38 AM.

  2. #17
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    The keys of a map are constant. That makes sense, because changing a key would destroy the ordering the map has on its keys. If you want to change a key, you need to remove the element from the map and re-insert it with the changed key.

    However, you probably don't want to do that. In your case, the problem is that display() is poorly written in that it is not marked const. Only const methods can be called on constant objects. They may not modify any members that not marked mutable, and they may not call non-const methods.

    To mark a method const, put "const" after its argument list:
    Code:
    class A
    {
      void b() const;
    };
    
    void A::b() const
    {
    }

    For inserting data into the map, you might want to look at this:
    http://www.boost.org/libs/assign/doc/index.html
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #18
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    In your case, the problem is that display() is poorly written in that it is not marked const. Only const methods can be called on constant objects. They may not modify any members that not marked mutable, and they may not call non-const methods.

    To mark a method const, put "const" after its argument list:
    Hey, thanks! Darn it. I figured that out for the operator< right underneath display():
    Code:
    void display()
    {
    	cout<<"Lit object: "<<i<<endl;
    }
    
    bool operator<(const Lit& rhs) const  //MUST BE CONSTANT
    {
    	return i < rhs.i;
    }
    However, my attempt at dealing with the 'const' part in the error I was getting led me in a different direction: I made the iterator a "const_iterator". But, when that didn't work, the error message had me thinking it was a dereferencing problem.

    Last edited by 7stud; 04-29-2005 at 11:52 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to assign a map iterator to an int variable?
    By Masterx in forum C++ Programming
    Replies: 22
    Last Post: 05-02-2009, 09:05 AM
  2. Polynomials and ADT's
    By Emeighty in forum C++ Programming
    Replies: 20
    Last Post: 08-19-2008, 08:32 AM
  3. Problem with static STL i.e. map
    By vijay_choudhari in forum C++ Programming
    Replies: 2
    Last Post: 10-26-2007, 05:56 AM
  4. Picture download problem
    By wingri in forum C Programming
    Replies: 10
    Last Post: 07-31-2007, 05:32 AM
  5. iterator copy problem
    By cdonlan in forum C++ Programming
    Replies: 2
    Last Post: 04-16-2005, 02:38 PM