title: C++ vector::_M_range_check Error?
body: <p>Here's my function:</p>
  
  <pre><code>void loadfromfile(string fn, vector&lt;string&gt;&amp; file){
      int x = 0;
      ifstream text(fn.c_str());
      while(text.good()){
          getline(text, file.at(x));
          x++;
      }
      //cout &lt;&lt; fn &lt;&lt; endl;
  }    
  </code></pre>
  
  <p>The value of fn that I'm passing in is just the name of a text file ('10a.txt')
  The value of file that I'm passing in is declared as follows:</p>
  
  <pre><code>vector&lt;string&gt; file1;
  </code></pre>
  
  <p>The reason I didn't define a size is because I didn't think I had to with vectors, they're dynamic... aren't they?</p>
  
  <p>This function is supposed to read a given text file and store the full contents of each line into a single vector cell.</p>
  
  <p>Ex. Store the contents of first line into file.at(0)
  Store the contents of the second line into file.at(1)
  And so on, until there aren't any more lines in the text file.</p>
  
  <p>The Error:</p>
  
  <p>terminate called after throwing an instance of 'std::out_of_range'
    what():  vector::_M_range_check</p>
  
  <p>I thought the check in the while loop should prevent this error!</p>
  
  <p>Thanks in advance for your help.</p>