title: C++ Multi-Dimensional Arrays bad excess code
body: <p>Im trying to create a matrix with different dimensions and it works fine for a 5 by 3 or a 3 by 3 but if i try making a 2 by 3 matrix then I get a bad excess code. In other words if i try to make my x smaller than my y in my (x,y) pair then I get bad excess.</p>
  
  <p>In my header file I have:</p>
  
  <pre><code>#ifndef fasdf_dynn_h
  #define fasdf_dynn_h
  
  #include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include&lt;string&gt;
  #include &lt;cstdlib&gt;
  #include &lt;vector&gt;
  
  using namespace std;
  template &lt;class T&gt;
  
  class MatrixdynVector{
  
  public:
      MatrixdynVector();
      MatrixdynVector(int m,int n);
      MatrixdynVector(T diagonal, int n, int m);
  
      template &lt;class H&gt;
      friend ostream&amp; operator &lt;&lt;(ostream&amp; outs, const MatrixdynVector&lt;H&gt; &amp;obj);
  
  private:
      int m,n;
      int** matrix;
  };
  
  
  #endif
  </code></pre>
  
  <p>and in my cpp file I have:</p>
  
  <pre><code>#include "dynn.h"
  
  template &lt;class T&gt;
  MatrixdynVector&lt;T&gt;::MatrixdynVector(){
  
      //creates a 3 by 3 matrix with elements equal to 0
      m=3;
      n=3;
  
      matrix=new int*[m];
  
      for(int i=0;i&lt;m;i++)
          matrix[i]=new int[n];
  
      for(int i=0;i&lt;m;i++)
          for(int j=0;j&lt;n;j++)
              matrix[i][j]=0;
  
  
  }
  
  template &lt;class T&gt;
  MatrixdynVector&lt;T&gt;::MatrixdynVector(int x,int y)
  {
      //creates a matrix with dimensions m and n with elements equal to 0
  
      m=x;
      n=y;
  
      matrix=new int*[m];
  
      for(int i=0;i&lt;m;i++)
          matrix[i]=new int[n];
  
      for(int i=0;i&lt;m;i++)
          for(int j=0;j&lt;n;j++)
              matrix[i][j]=0;
  }
  
  
  template &lt;class T&gt;
  ostream&amp; operator &lt;&lt;(ostream&amp; outs, const MatrixdynVector&lt;T&gt; &amp; obj)
  {
      for (int i = 0; i &lt; obj.m; i++){
          for (int j = 0; j &lt; obj.n; j++)
              outs &lt;&lt; " "&lt;&lt; obj.matrix[i][j];
          outs&lt;&lt;endl;
      }
      outs&lt;&lt;endl;
      return outs;
  }
  
  int main()
  {
      MatrixdynVector&lt;int&gt; B;//works fine
      MatrixdynVector&lt;int&gt; A(2,3);//bad excess 
  
  
      cout&lt;&lt;B;
      cout&lt;&lt;A;
  }
  </code></pre>