Hi,

I have a problem compiling a project in Dev-C++ (MingW32). For some reason I can't assign an iterator from map::erase(iterator). My sources (MSDN) for map::erase(iterator) says that erase(iterator) returns an iterator... Does anyone know what I'm doing wrong?

bool TCPIPRedirectManager::poll(){
   bool haveMsg = false;
   
   for(map<ConnHandle, TCPIPRedirect*>::iterator it = openPorts.begin(); it != openPorts.end(); ){
      if(it->second->poll()){
         haveMsg = true;
         }
      else if(it->second->shouldClose()){
         delete it->second;            // close the port   
         it = openPorts.erase(it);          // <-- ##THIS IS THE PROBLEM LINE##
         continue;                     // go to top - don't inc iterator
         }
      
      it++;
      }
      
   return haveMsg;
   }

The error message is shown here, the line it's talking about is the iterator assignment line.

In member function `bool TCPIPRedirectManager::poll()':

no match for 'operator=' in 'it = ((std::map<ConnHandle, TCPIPRedirect*, std::less<ConnHandle>, std::allocator<std::pair<const ConnHandle, TCPIPRedirect*> > >*)this)->std::map<_Key, _Tp, _Compare, _Alloc>::erase [with _Key = ConnHandle, _Tp = TCPIPRedirect*, _Compare = std::less<ConnHandle>, _Alloc = std::allocator<std::pair<const ConnHandle, TCPIPRedirect*> >](it)'

note C:\Dev-Cpp\include\c++\3.4.2\bits\stl_tree.h:152 candidates are: std::_Rb_tree_iterator<std::pair<const ConnHandle, TCPIPRedirect*> >& std::_Rb_tree_iterator<std::pair<const ConnHandle, TCPIPRedirect*> >::operator=(const std::_Rb_tree_iterator<std::pair<const ConnHandle, TCPIPRedirect*> >&)

> My sources (MSDN) for map::erase(iterator) says that erase(iterator) returns an iterator ...

this is the interface given by dinkumware (and microsoft) for map<>::erase.

iterator erase(iterator where);
iterator erase(iterator first, iterator last);
size_type erase(const Key& keyval);

and this is the interface as per gcc (libstdc++)

void erase(iterator where);
void erase(iterator first, iterator last);
size_type erase(const Key& keyval);

for the first two functions, the dinkumware/microsoft signatures do not conform to c++98;
the gnu versions are the ones conforming to the standard.

however, the dinkum/microsoft signature for the first erase overload

iterator erase(iterator where);

would become the conforming one for c++0x.
see: http://anubis.dkuug.dk/jtc1/sc22/wg21/docs/lwg-active.html#130

you can ignore the whole issue regarding the return type by writing the function this way

bool TCPIPRedirectManager::poll(){
   bool haveMsg = false;
   
   for(map<ConnHandle, TCPIPRedirect*>::iterator it = openPorts.begin(); it != openPorts.end(); ){
      if(it->second->poll()){
         haveMsg = true;
         }
      else if(it->second->shouldClose()){
         delete it->second;            // close the port   
         //it = openPorts.erase(it);          // <-- ##THIS IS THE PROBLEM LINE##
         openPorts.erase( it++ ) ; // conforming
         continue;                     // go to top - don't inc iterator
         }
      
      it++;
      }
      
   return haveMsg;
   }
This article has been dead for over six months. Start a new discussion instead.