title: Vector of object from a custom class
body: <p>In C++, I declare a custom class to store some values for an object. Then, I declare a vector of said object. Finally, I iterate through the vector to assign values to the fields.</p>
  
  <pre><code>#include &lt;vector&gt;
  
  using namespace std;
  
  class Custom
  { 
      public:
          int metric,nX,nY;
      private:
  
  };
  
  int main( int argc, char** argv )
  {
  
  vector&lt;Custom&gt; MyWonderfulVector;
  
  // Some code//
  
  for(int i=0 ; i&lt;10 ; i++){
  
  MyWonderfulVector[i].metric = computation1();
  MyWonderfulVector[i].nX= computation2();
  MyWonderfulVector[i].nY= computation3();
  }
  
  return 0;
  
  }
  </code></pre>
  
  <p>It throws a <code>vector subscript out of range</code> when it tries to evaluate <code>MyWonderfulVector[i].metric = computation1();</code>. <code>metric</code> is an int and <code>computation1()</code> too. at the first iteration, i=0 so it should be ok. Curiously, somewhere else in the code, I have a vector of another class (included in a library) and this syntax works for it, so I don't understand why it doesn't work here.</p>
  
  <p><strong>EDIT :</strong> </p>
  
  <p>Ok with the comments I changed to following line:     vector MyWonderfulVector(10);</p>
  
  <p>So my problem is that I did not initialize the size of the vector (bad habit from Matlab ;) ) From what I understand, if I don't initialize the vector's to a fixed size, I must push_back the objects to "increase" the size of the vector. So, I should create a temporary Custom Object to assign the fields, then push_back this temp object into the vector. If one of the commenter wants to put this into an answer...</p>