title: C++ function not working when called (OOP/matrix class)
body: <p>So I'm currently working on my bachelor thesis and I need to code a class hierarchy (using vector and matrix classes) in order to use methods for solving linear systems.</p>
  <p>I coded the basic functions for both classes, and even tried them and they were working. But as I started to add more to the code, I noticed that the functions I used for adding elements to a matrix and displaying them basically stopped working although I don't think I modified them in any way (the same functions I used for the vector class still work).</p>
  <p>In a way the functions work, because the couts are displayed, but I don't get to input the values at all.</p>
  <p>Here's the vector class (and before you say anything, I don't want to use template):</p>
  <pre><code>#include &lt;iostream&gt;
  using namespace std;
  #pragma once
  
  class vector {
  protected:
      int n;
      double* vect;
  
  public:
      vector() {
          n = 1;
          vect = new double[n];
          for (int i = 0; i &lt; n; i++) {
              vect[i] = 0;
          }
      }
  
      vector(int dim) {
          n = dim;
          vect = new double[n];
          for (int i = 0; i &lt; n; i++) {
              vect[i] = 0;
          }
      }
  
      vector(const vector&amp; v) {
          n = v.n;
          vect = new double[n];
          for (int i = 0; i &lt; n; i++) {
              vect[i] = v.vect[i];
          }
      }
  
      ~vector() {
          delete[] vect;
      }
  
      int getsize() {
          return n;
      };
  
      const vector&amp; operator=(const vector&amp;);
      bool operator== (const vector&amp;) const;
      bool operator!= (const vector&amp;) const;
      void read();
      void display();
      friend ostream&amp; operator&lt;&lt;(ostream&amp;, const vector&amp;);
      friend istream&amp; operator&gt;&gt;(istream&amp;, const vector&amp;);
  };
  
  const vector&amp; vector::operator= (const vector&amp; c) {
      if (this == &amp;c)
          return *this;
  
      if (this != &amp;c) {
          if (n != c.n) {
              delete[] vect;
              n = c.n;
              vect = new double[n];
          }
          for (int i = 0; i &lt; n; i++) {
              vect[i] = c.vect[i];
          }
      }
      return *this;
  }
  
  bool vector::operator== (const vector&amp; c) const {
      if (n != c.n)
          return false;
      for (int i = 0; i &lt; n; i++) {
          if (vect[i] != c.vect[i])
              return false;
      }
      return true;
  }
  
  bool vector::operator!= (const vector&amp; c) const {
      return !(*this == c);
  }
  
  void vector::read() {
      cout &lt;&lt; &quot;Insert vector elements:&quot;;
      for (int i = 0; i &lt; n; i++) {
          cin &gt;&gt; vect[i];
      }
  }
  
  void vector::display() {
      cout &lt;&lt; &quot;The vector is: &quot;;
      for (int i = 0; i &lt; n; i++) {
          cout &lt;&lt; vect[i];
          cout &lt;&lt; &quot; &quot;;
      }
  }
  
  ostream&amp; operator&lt;&lt;(ostream&amp; out, const vector&amp; v) {
      for (int i = 0; i &lt; v.n; i++) {
          out &lt;&lt; &quot; &quot; &lt;&lt; v.vect[i];
      }
      return out;
  }
  
  istream&amp; operator&gt;&gt;(istream&amp; in, const vector&amp; v) {
      for (int i = 0; i &lt; v.n; i++) {
          in &gt;&gt; v.vect[i];
      }
      return in;
  }
  </code></pre>
  <p>and the matrix class is this</p>
  <pre><code>#include &quot;vector.h&quot;
  
  class matrix {
  private:
      int n;
      vector** mat;
  public:
      matrix() {
          n = 2;
          mat = new vector * [n];
          for (int i = 0; i &lt; n; i++) {
              mat[i] = new vector[n];
              for (int j = 0; j &lt; n; j++) {
                  mat[i][j] = 0;
              }
          }
  
      }
  
      matrix(int dim) {
          n = dim;
          mat = new vector * [n];
          for (int i = 0; i &lt; n; i++) {
              mat[i] = new vector[n];
              for (int j = 0; j &lt; n; j++) {
                  mat[i][j] = 0;
              }
          }
  
      }
  
      matrix(const matrix&amp; m) {
          n = m.n;
          mat = new vector * [n];
          for (int i = 0; i &lt; n; i++) {
              mat[i] = new vector[n];
              for (int j = 0; j &lt; n; j++)
                  mat[i][j] = m.mat[i][j];
          }
      }
  
      ~matrix() {
          for (int i = 0; i &lt; n; i++)
              delete[] mat[i];
          delete[] mat;
      }
  
      friend ostream&amp; operator&lt;&lt;(ostream&amp;, const matrix&amp;);
      friend istream&amp; operator&gt;&gt;(istream&amp;, const matrix&amp;);
      void read();
      void display();
      const matrix&amp; operator=(const matrix&amp;);
  
  };
  
  const matrix&amp; matrix::operator=(const matrix&amp; m) {
      if (this == &amp;m)
          return *this;
  
      if (this != &amp;m) {
          for (int i = 0; i &lt; n; i++) {
              delete[] mat[i];
          }
          delete[] mat;
          n = m.n;
          mat = new vector*[n];
          for (int i = 0; i &lt; n; i++) {
              mat[i] = new vector[n];
          }
  
          for (int i = 0; i &lt; n; i++) {
              for (int j = 0; j &lt; n; j++) {
                  mat[i][j] = m.mat[i][j];
              }
          }
      }
      return *this;
  }
  
  ostream&amp; operator&lt;&lt;(ostream&amp; out, const matrix&amp; m) {
      for (int i = 0; i &lt; m.n; i++) {
          out &lt;&lt; m.mat[i][0];
          for (int j = 1; j &lt; m.n; j++) {
              out &lt;&lt; &quot; &quot; &lt;&lt; m.mat[i][j];
          }
          out &lt;&lt; endl;
      }
      return out;
  }
  
  istream&amp; operator&gt;&gt;(istream&amp; in, const matrix&amp; m) {
      for (int i = 0; i &lt; m.n; i++) {
          for (int j = 0; j &lt; m.n; j++)
              in &gt;&gt; m.mat[i][j];
      }
      return in;
  }
  
  void matrix::read() {
      cout &lt;&lt; &quot;Insert matrix elements:&quot;&lt;&lt;endl;
      for (int i = 0; i &lt; n; i++) {
          for (int j = 0; j &lt; n; j++)
              cin &gt;&gt; mat[i][j];
      }
  }
  
  void matrix::display() {
      cout &lt;&lt; &quot;The matrix is:&quot;;
      for (int i = 0; i &lt; n; i++) {
          for (int j = 0; j &lt; n; j++)
              cout &lt;&lt; mat[i][j];
      }
      cout &lt;&lt; endl;
  }
  </code></pre>
  <p>Here's the source as well</p>
  <pre><code>#include &quot;vector.h&quot;
  #include &quot;matrix.h&quot;
  
  int main() {
      vector v1(3);
      v1.read();
      v1.display();
      v1.~vector();
      cout &lt;&lt; endl;
  
      matrix m2(2);
      m2.read();
      m2.display();
      m2.~matrix();
      system(&quot;pause&quot;);
      return 0;
  }
  </code></pre>
  <p>Edit:</p>
  <p><a href="https://i.stack.imgur.com/nIUUw.png" rel="nofollow noreferrer">I added a picture for clarification</a></p>
  <p>Edit 2:
  @user4581301 pointed out that I accidentally made a 3D vector, so I turned <code>vector** mat</code> into <code>vector* mat</code> and created an initialization function in the vector class to help me initialize a vector of vectors.
  Now it works! Thanks again!</p>