title: Vector erase iterator outside of range
body: <p><a href="https://drive.google.com/open?id=0B7MaiC_Cj3XsUFJRZG1rRXdwQTA" rel="nofollow">link</a> to the project 
  I am creating a small glut application that will draw circuit components.
  I have a class called "Component" that looks like that:</p>
  
  <pre><code>// nextIndex is the index of the next component to be created
  int Component::nextIndex = 0;
  Component::Component( void ){
  setType( RESISTOR );
  setValue( 0.0 );
  setX( 0.0 );
  setY( 0.0 );
  setSize( 2.0 );
  setIndex( nextIndex );
  }
  
  eType Component::getType( void ){return cType}
  double Component::getValue( void ){return cValue;}
  double Component::getX( void ){return cX;}
  double Component::getY( void ){return cY;}
  double Component::getSize( void ){return cSize;}
  void Component::setType( eType type ){cType = type;}
  void Component::setValue( double value ){cValue = value;}
  void Component::setX( double x){cX = x;}
  void Component::setY( double y ){cY = y;}
  void Component::setSize( double size ){cSize = size;}
  int Component::getIndex( void ){return index;}
  void Component::setIndex( int n ){index = n;}
  </code></pre>
  
  <p>NOTE: I am incrementing the nextIndex every time I create a new instance of the class.
  Here is my other class which has a vector of Components and handles the addition and deletion of the components:</p>
  
  <pre><code>class OGWindow{
  private:
  
      bool LINK, DELETE;
      int linkNo;
      ...
      Component currentSquare;
      Component *currentSquarePtr, *linkSquPtr1, *linkSquPtr2;
      double squareXStart, squareYStart, squareXEnd, squareYEnd;
      std::vector&lt; Component &gt; compVector;
  
  public:
      ...
      void myMouseClick(int button, int state, int x, int y);
      ...
      void deleteSquare( Component *squPtr );
  };
  </code></pre>
  
  <p>I am creating components to the window fine like that:
      int NEXT_INT = 0;</p>
  
  <pre><code>if (x &gt;= createButtonX &amp;&amp; x &lt;= createButtonX + createButtonWidth) {
      cout &lt;&lt; "Clicked Create button" &lt;&lt; endl;
      Component *compPtr = new Component;
      compPtr-&gt;setX(0.0);
      compPtr-&gt;setY(0.0);
      compPtr-&gt;setIndex(NEXT_INT);
      NEXT_INT++;
  
      compVector.push_back(*compPtr);
      //------------SET THE CURRENT SQUARE TO THE NEWLY CREATED COMPONENT-----
      vector&lt;Component&gt;::iterator constIterator;
      for (constIterator = compVector.begin(); constIterator != compVector.end(); ++constIterator){
          currentSquarePtr = &amp;(*constIterator);
      }
      cout &lt;&lt; "Vector size:  " &lt;&lt; compVector.size() &lt;&lt; endl;
      glutPostRedisplay();
  }
  </code></pre>
  
  <p>But when I am deleting, it deletes some of the elements and at the last one I get the "vector out of range" error:</p>
  
  <pre><code>void OGWindow::deleteSquare( Component *squPtr ) {
      compVector.erase(compVector.begin() + (squPtr-&gt;getIndex()));
  cout &lt;&lt; "Vector size:  " &lt;&lt; compVector.size() &lt;&lt; endl;
  glutPostRedisplay();
  }
  </code></pre>
  
  <p>Please share any ideas why this is happening.</p>