title: Unhandled exception at location :Microsoft C++ exception: std::length_error at memory location
body: <p>There haven't really been any answers that I could understand after looking through the questions that were previously asked on this subject.  My TA tried to help me with this for about an hour and couldn't figure anything out.</p>
  
  <p>First of all, this is where the error takes me when the program compiles</p>
  
  <p>In xthrow.cpp</p>
  
  <pre><code>_CRTIMP2_PURE __declspec(noreturn) void __CLRCALL_PURE_OR_CDECL
  _Xlength_error(_In_z_ const char * _Message)
  { // report a length_error
  _THROW_NCEE(length_error, _Message);
  }
  </code></pre>
  
  <p>In dbgmalloc.c</p>
  
  <pre><code>void *res = _nh_malloc_dbg(nSize, _newmode, _NORMAL_BLOCK, NULL, 0);
  </code></pre>
  
  <p>Header file</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;cmath&gt; 
  #include &lt;vector&gt;
  #include &lt;string&gt;
  #include &lt;iomanip&gt;
  
  using namespace std;
  
  class matrix
  {   
  private:
      vector &lt;vector &lt;double&gt; &gt; mat;      // Stores 2D matrices
      int nRows;                      // Stores number of rows
      int nCols;                      // Stores number of columns
      void resize(int nRows, int nCols);          // Resizes mat      
  
  public:
      void readfile(ifstream&amp;);           // Reads values for nRows and nCols
      matrix(ifstream&amp;);                  // Calls readfile function
      void print();                       // Prints the current values of mat
      int getrows();                      // Returns the value of nRows
      int getcols();                      // Returns the value of nColumns
      vector&lt;vector&lt;double&gt;&gt; getmat();        // Returns a 2D vector
      void add(matrix);                   // Adds 2 matrices and stores in mat 
      // Multiplies 2 matrices and stores in mat
      vector&lt;vector&lt;double&gt;&gt; multiply(matrix);
  };
  </code></pre>
  
  <p>function file</p>
  
  <pre><code>#include "matrix.h"
  
  using namespace std;
  
  void matrix::resize(int nRows, int nCols)
  {
      mat.resize(nRows);
      for(int i=0; i&lt;nRows; i++)
      {
          mat[i].resize(nCols);
      }
  }
  
  void matrix::readfile(ifstream&amp; input_file)
  {
  
  input_file &gt;&gt; nRows &gt;&gt; nCols;
  
  resize(nRows, nCols);
  
  for(int i=0; i&lt;nRows; i++)
  {
      for(int j=0; j&lt;nCols; j++)
      {
          input_file &gt;&gt; mat[i][j];
      }
  }
  }
  
  matrix::matrix(ifstream&amp; input_file)
  {
      readfile(input_file);
  }
  
  void matrix::print()
  {
  for(int i=0; i&lt;nRows; i++)
  {
      for(int j=0; j&lt;nCols; j++)
      {
          cout &lt;&lt; mat[i][j] &lt;&lt; '\t';
      }
  cout &lt;&lt; endl;
  }
  
  }
  </code></pre>
  
  <p>main </p>
  
  <pre><code>#include "matrix.h"
  
  using namespace std;
  
  int main()
  {
  
  ifstream input_file;
  string filename;
  //int nRows;
  //int nCols;
  
  cout &lt;&lt; "Enter filename: " &lt;&lt; endl;
  cin &gt;&gt; filename;
  cout &lt;&lt; '\n';
  
  input_file.open(filename.c_str());
  if (input_file.fail())
  {
      cout &lt;&lt; "file didn't open.";
      system("pause");
  }
  else
  {
  //input_file &gt;&gt; nRows &gt;&gt; nCols;
  //cout &lt;&lt; "Matrix 1:" &lt;&lt; endl &lt;&lt; "Rows = " &lt;&lt; getrows() &lt;&lt; endl;
  //cout &lt;&lt; "Columns = " &lt;&lt; nCols &lt;&lt; '\n' &lt;&lt; endl;
  
  matrix a(input_file);
  a.print();
  system("pause");
  }
  return 0;
  }
  </code></pre>
  
  <p>Sorry the format is a little nasty.  It is difficult to copy and paste code into this...  Thanks for your help!</p>