title: C++ STL vector.erase() always deletes last element
body: <p>Using: Eclipse 3.8.1, C/C++ Remote Debug Launcher 6.0.0, with GCC 1.1.0, and GDB 7.0.0
  Verified on: Visual Studio 2010</p>
  
  <p>I am pulling my hair out on this one.  When using the following code, the last element of the vector of Room objects <em>rooms</em> is always removed.  I cannot get any element other than the last to erase with this code.  I am only trying to remove one element (the location is determined by the user).</p>
  
  <p>I know that many of you are probably asking why I don't use a list, but random data access is much more important than ease of element addition / removal with this collection.  Do you have any ideas?</p>
  
  <pre><code>void House::removeRoom(unsigned int roomToRemove){
      try{
          if(roomDoesNotExist(roomToRemove)) throw houseException("Room requested to     remove does not exist", roomToRemove, __FILE__, __LINE__);
  
          vector&lt;Room&gt;::iterator roomIterator = rooms.begin() + roomToRemove;
          rooms.erase(roomIterator);
  
          removeAllLinksToRoom(roomToRemove);
          renumberLinkedRoomsAfterErase(roomToRemove);
      }
      catch(houseException&amp; hException){
          hException.display(cerr);
      }
  }
  </code></pre>
  
  <p>Here is a snippet of the House class:</p>
  
  <pre><code>#include "Room.h"
  
  using namespace std;
  
  class House {
  public:
      vector&lt;Room&gt; rooms;
  
      //member functions
      void removeRoom(unsigned int roomToRemove);
  
  
      //constuctors / destructors
      House();
      virtual ~House();
  
      //STL linked list overload operators =, ==, and &lt;
      House &amp;operator=(const House &amp;rhs);
      int operator==(const House &amp;rhs) const;
      int operator&lt;(const House &amp;rhs) const;
  
  private:
      bool roomDoesNotExist(int roomToRemove);
      void removeAllLinksToRoom(int roomToUnlink);
      void renumberLinkedRoomsAfterErase(int erasedRoom);
  };
  </code></pre>
  
  <p>Because it could be relevant, I included my entire Room class:</p>
  
  <pre><code>#include "Wall.h"
  
  #include &lt;vector&gt;
  #include &lt;algorithm&gt;
  #include &lt;string&gt;
  
  #include "exceptions/houseException.h"
  
  using namespace std;
  
  class Room {
      public:
      float setPointDegF;
      vector&lt;Wall&gt; walls;  //TODO consider making walls protected / private
  
      private:
      string roomName;
      vector&lt;int&gt; linkedRooms;
      float storedTemperature;
      float storedHumidity;
  
      //member functions
      void linkToRoom(int roomToLink);
      void unlinkFromRoom(int roomToUnlink);
  
      void removeAllLinksToRoom(int roomToUnlink);
      void renumberLinkedRoomsAfterErase(int erasedRoom);
  
  public:
      //friends
      friend class House;
  
      //member functions
      void addWalls(unsigned int numWallsToAdd=1);
      void removeWall(unsigned int wallToRemove);
  
      //Sensor Functions
      void readSensorTemperature();
      void readSensorHumidity();
  
      void temperature();
      void humidity();
  
      //constuctors / destructors
      Room();
      virtual ~Room();
  //STL linked list overload operators =, ==, and &lt;
      Room &amp;operator=(const Room &amp;rhs);
      int operator==(const Room &amp;rhs) const;
      int operator&lt;(const Room &amp;rhs) const;
  private:
      void getAttachedRooms(Wall&amp; tempWall);
  
      bool wallDoesNotExist(unsigned int wallToRemove);
      bool roomLinked(int roomToLink);
      bool roomNotLinked(int roomToLink);
  
      vector&lt;int&gt;::iterator findRoom(int roomToFind);
      vector&lt;int&gt;::iterator findInsertionPoint(int roomToInsert);
  
  
  };
  </code></pre>