Note that there are some explanatory texts on larger screens.

plurals
  1. POAddition of 2D Dynamic Arrays
    primarykey
    data
    text
    <p>I have a class project that is to make and operate on dynamic objects. I have a class called Matrix that uses a 2 dimensional pointer array to store object of type Complex (which is a complex number class). I need to be able to add 2 arrays by adding all of the values in the arrays together and returning a new array. The issue is that I don't understand the syntax for accessing each Complex object in the array. Here is what I have so far for the overloaded addition operator:</p> <pre><code>const Matrix Matrix::operator+(const Matrix&amp; rhs) const { Matrix newMatrix(mRows,mCols); for(int i=0;i&lt;mRows;i++) { for(int j=0;j&lt;mCols;j++) { (*newMatrix.complexArray[i]) = (*complexArray[i])+ (*rhs.complexArray[i]); } } return newMatrix; } </code></pre> <p>Here is the overloaded input operator for the Matrix object:</p> <pre><code>istream&amp; operator&gt;&gt;(istream&amp; input, Matrix&amp; matrix) { bool inputCheck = false; int cols; while(inputCheck == false) { cout &lt;&lt; "Input Matrix: Enter # rows and # columns:" &lt;&lt; endl; input &gt;&gt; matrix.mRows &gt;&gt; cols; matrix.mCols = cols/2; //checking for invalid input if(matrix.mRows &lt;= 0 || cols &lt;= 0) { cout &lt;&lt; "Input was invalid. Try using integers." &lt;&lt; endl; inputCheck = false; } else { inputCheck = true; } input.clear(); input.ignore(80, '\n'); } if(inputCheck = true) { cout &lt;&lt; "Input the matrix:" &lt;&lt; endl; for(int i=0;i&lt; (matrix.mRows+matrix.mCols);i++) { Complex* newComplex = new Complex(); input &gt;&gt; *newComplex; matrix.complexArray[i] = newComplex; } } return input; } </code></pre> <p>Here is the Matrix class definition:</p> <pre><code>class Matrix { friend istream&amp; operator&gt;&gt;(istream&amp;, Matrix&amp;); friend ostream&amp; operator&lt;&lt;(ostream&amp;, const Matrix&amp;); private: int mRows; int mCols; static const int MAX_ROWS = 10; static const int MAX_COLUMNS = 15; Complex **complexArray; public: Matrix(int=0,int=0); Matrix(Complex&amp;); ~Matrix(); Matrix(Matrix&amp;); Matrix&amp; operator=(const Matrix&amp;); const Matrix operator+(const Matrix&amp;) const; }; </code></pre> <p>And the constructor:</p> <pre><code>Matrix::Matrix(int r, int c) { if(r&gt;0 &amp;&amp; c&gt;0) { mRows = r; mCols = c; } else { mRows = 0; mCols = 0; } if(mRows &lt; MAX_ROWS &amp;&amp; mCols &lt; MAX_COLUMNS) { //complexArray= new Complex[mRows]; complexArray= new Complex*[mRows]; for(int i=0;i&lt;mRows;i++) { complexArray[i] = new Complex[mCols]; } } } </code></pre> <p>As it is now, the program compiles, but stops working when it gets to the addition of matrices during runtime. If anyone can tell me what syntax I should be using and why, it would be extremely helpful. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload