title: Sum of matrices using dynamic allocation and operator overloading
body: <p>I have written a code to find sum of two matrices using dynamic allocation and operator overloading.</p>
  
  <pre><code>#include&lt;iostream&gt;
  #include&lt;new&gt;
  using namespace std;
  class matrix
  {
      int**m;
      int r;
      int c;
  public:
      matrix(int a, int b)
      {
  
          r = a;
          c = b;
          m = new int*[r];
          for (int i = 0; i &lt; r; i++)
              m[i] = new int[c];
      }
      ~matrix()
      {
          for (int i = 0; i &lt; r; i++)
              delete[] m[i];
          delete[] m;
      }
      friend istream &amp;operator&gt;&gt;(istream &amp;in, matrix s);
      friend matrix operator+(matrix&amp; m1, matrix&amp; m2);
      friend ostream &amp;operator&lt;&lt;(ostream &amp;out, matrix s);
  
  };
  istream &amp;operator&gt;&gt;(istream &amp;in, matrix s)
  {
  
      cout &lt;&lt; "enter elements" &lt;&lt; endl;
      for (int i = 0; i &lt; s.r; i++)
      {
          for (int j = 0; j &lt; s.c; j++)
          {
              in &gt;&gt; s.m[i][j];
          }
      }
      return in;
  }
  ostream &amp;operator&lt;&lt;(ostream &amp;out, matrix s)
  {
  
      for (int i = 0; i &lt; s.r; i++)
      {
  
          cout &lt;&lt; " ";
          for (int j = 0; j &lt; s.c; j++)
              out &lt;&lt; s.m[i][j] &lt;&lt; " ";
          cout &lt;&lt; endl;
      }
      return out;
  }
  matrix operator+(matrix&amp; m4, matrix&amp; m5)
  {
      matrix m6(m4.r, m4.c);
      for (int i = 0; i &lt; m4.r; i++)
      {
          for (int j = 0; j &lt; m4.c; j++)
          {
              m6.m[i][j] = m4.m[i][j] + m5.m[i][j]; //gets stuck here!
          }
  
      }
      return m6;
  }
  int main()
  {
      int r, c;
      cout &lt;&lt; "enter number of rows and columns" &lt;&lt; endl;
      cin &gt;&gt; r &gt;&gt; c;
      matrix m1(r, c), m2(r, c), m3(r, c);
      cin &gt;&gt; m1;
      cin &gt;&gt; m2;
      m3 = m1 + m2;
      cout &lt;&lt; m3;
      return 0;
  }
  </code></pre>
  
  <p>When I execute, I get stuck at <code>matrix operator+(matrix &amp;m4,matrix &amp;m5)</code>.</p>
  
  <p>I searched on internet but I'm unable to find my error.So, what's wrong with my code? It works fine in Code::Blocks but not in Xcode. </p>