CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Aug 2002
    Location
    Madrid
    Posts
    4,588

    STLPort multimap problem or mine ?

    It is probably my problem, but I don't see what's wrong here :/

    I'm trying to delete a specifc element in a multimap based on the key and value since the multimap can store multiple entries with the same key.

    Code:
    #include <map>
    using namespace std;
    
    struct eqstr
    {
      bool operator()(const char* s1, const char* s2) const
      {
        return strcmp(s1, s2) == 0;
      }
    };
    
    typedef multimap<const char*, int, eqstr> map_type;
    
    void eraseElem(const map_type& Map, const char* str, const int key)
    {
      pair<map_type::const_iterator, map_type::const_iterator> p = Map.equal_range(str);
      map_type::const_iterator it = p.first;
      while ((it != p.second) && ((*it).second != key)) {
        ++it;
      }
      if (it != p.second) {
        Map.erase(it); // <- this line doesn't compile
      }
    }
    
    int main()
    {
      map_type M;
      M.insert(map_type::value_type("H", 1));
      M.insert(map_type::value_type("H", 2));
      M.insert(map_type::value_type("C", 12));
      M.insert(map_type::value_type("C", 13));
      M.insert(map_type::value_type("O", 16));
      M.insert(map_type::value_type("O", 17));
      M.insert(map_type::value_type("O", 18));
      M.insert(map_type::value_type("I", 127));
    
      eraseElem(M, "O", 18);
      return 0;
    }
    The error I get is :
    'erase' : 3 overloads have no legal conversion for 'this' pointer

    I have tried it both with const_iterator and iterator. I can't really see what is wrong, since the only condition that is documented is that it is a dereferenceable iterator in Map. Or is this iterator not in Map ?

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449
    Remove the "const" from your function prototype.
    Code:
    void eraseElem(map_type& Map, const char* str, const int key)
    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Aug 2002
    Location
    Madrid
    Posts
    4,588
    Thanks a lot

    Ups that was a silly one

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured