title: C++ vector segmentation error when accessing the first element
body: <p>I've been having a really hard time with this error, as I've tried everything I could to fix it to no avail.</p>
  
  <p>I'm building a genetic algorithm for my C++ class and I have to store a lot of individuals in a container of some sort, so I chose to make a custom container class called "GenePool" to hold instances of "IndivPtr" (which is a typedef smart pointer to "Individual").</p>
  
  <p>These individuals are stored in its internal vector, and I overloaded the subscript operator ([]) to access its elements.</p>
  
  <p>However, my program barely runs because after filling the vector with elements, it always causes a segmentation error when trying to access the FIRST element from the vector, and it throws an std::out_of_range exception!</p>
  
  <p>I would like to know in this case how I can access elements from my vectors without causing such error.</p>
  
  <p>Here is the code for GenePool:</p>
  
  <pre><code>#include "GenePool.h"
  #include "Controller.h"
  #include &lt;algorithm&gt;
  
  GenePool::GenePool()
  {
      // Default empty constructor
      individualList.reserve(10000);
  }
  
  GenePool::~GenePool()
  {
      //deleteAll();
  }
  
  void GenePool::sortPool()
  {
      // Sort the vector from greatest to least using GreatertThanSort
      // The third parameter is the address of the GreaterThanSort's greater than function for a GreaterThanSort for Individuals
      std::sort(individualList.begin(), individualList.end(), &amp;GreaterThanSort::greaterThan);
  }
  
  Individual&amp; GenePool::operator[](int index)
  {
      // Put exception handling here somewhere (a throw statement)
      return *individualList.at(index);
  }
  
  // Get an individual from the list between index 0 and index size - 1
  Individual&amp; GenePool::getRandIndiv()
  {
      return this-&gt;operator[](Controller::getRandNumInRange(0, this-&gt;size() - 1));
  }
  
  void GenePool::pushBackIndiv(const IndivPtr&amp; indiv)
  {
      individualList.push_back(indiv);
  }
  
  void GenePool::pushBackIndiv(Individual&amp; indiv)
  {
      Individual * p2Indiv = &amp;indiv;
  
      if(LangermannPoint * pIndivL = dynamic_cast&lt;LangermannPoint*&gt;(p2Indiv))
      {
          IndivPtr pL(new LangermannPoint(*pIndivL));
          individualList.push_back(pL);
      }
      else if(CurveParams * pIndivC = dynamic_cast&lt;CurveParams*&gt;(p2Indiv))
      {
          IndivPtr pC(new CurveParams(*pIndivC));
          individualList.push_back(pC);
      }
  }
  
  int GenePool::size() const
  {
      return individualList.size();
  }
  
  void GenePool::clear()
  {
      if(!individualList.empty())
      {
          individualList.clear();
      }
  }
  
  void GenePool::addContentsOf(GenePool&amp; other)
  {
      for(int i = 0; i &lt; other.size(); ++i)
      {
          pushBackIndiv(other[i]);
      }
  }
  </code></pre>
  
  <p>Before this subscript is called, the vector is filled:</p>
  
  <pre><code>    // Initialize a population of individuals with randomly generated     parameters.
      if(getProblemType() == Controller::OPTIMIZATION)
      {
          for(int i = 0; i &lt; getInitPopSize(); ++i)
          {
              population.pushBackIndiv(IndivPtr(new LangermannPoint(getRandFloatInRange(0.0f, LangermannPoint::POINT_BOUND),
                                                                    getRandFloatInRange(0.0f, LangermannPoint::POINT_BOUND))));
          }
      }
      else
      {
          for(int i = 0; i &lt; getInitPopSize(); ++i)
          {
              population.pushBackIndiv(IndivPtr(new CurveParams(getRandFloatInRange(-CurveParams::PARAM_BOUND, CurveParams::PARAM_BOUND),
                                                                getRandFloatInRange(-CurveParams::PARAM_BOUND, CurveParams::PARAM_BOUND),
                                                                getRandFloatInRange(-CurveParams::PARAM_BOUND, CurveParams::PARAM_BOUND),
                                                                getRandFloatInRange(-CurveParams::PARAM_BOUND, CurveParams::PARAM_BOUND))));
          }
      }
  </code></pre>
  
  <p>Here is the invocation to the subscript operator that always crashes:</p>
  
  <pre><code>bool Controller::terminationCondition()
  {
      population.sortPool();
      // After sorting, the first is the fittest
      if(generationCount &lt;= 1)
      {
          setSolution(population[0]);
          return false;
      }
      else if(getSolution() &lt; population[0] &amp;&amp; generationCount &lt; MAX_GEN_COUNT)
      {
          setSolution(population[0]);
          return false;
      }
      else
      {
          return true;
      }
  
  }
  </code></pre>