Matrix assignment operator

I am trying to overload the assignment operator. My code is to multiply, add, and multiply matrices, but as I am doing this step by step, I am not able to successfully overload this operator and I do not understand why. I know there is something I am overlooking but I can't see it. :(
Here is the relevant code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
class matrixType
{
  friend ostream& operator<<(ostream&, const matrixType&);
  friend istream& operator>>(istream&, matrixType&);
public:
  const matrixType& operator=(const matrixType& right);
  void setMatrix();
  ~matrixType() { delete [] matrix;}
  matrixType();
  matrixType(int rows, int columns);
  matrixType(const matrixType& copy); // copy constructor                           
  matrixType operator+(matrixType right);
  matrixType operator-(matrixType right);
  matrixType operator*(matrixType right);
private:
  int matrix[100][100];
  int rowSize;
  int columnSize;
};

const matrixType& matrixType::operator=(const matrixType &right)
{
  rowSize = right.rowSize;
  columnSize = right.columnSize;
  for (int i=0; i<rowSize; i++)
    {
      for (int j=0; j<columnSize; i++)
        matrix[i][j]= right.matrix[i][j];
    }
}
matrixType matrixType::operator+(matrixType right)
{
  matrixType m;
  m.rowSize = right.rowSize;
  m.columnSize = right.columnSize;
  for (int i=0; i < rowSize; i++)
    {
      for (int j=0; j< columnSize; j++)
        m.matrix[i][j]= matrix[i][j] + right.matrix[i][j];
    }
  return m;
}
// copy constructor
matrixType::matrixType(const matrixType &copy) 
{
  rowSize = copy.rowSize;
  columnSize = copy.columnSize;
  for (int i=0; i< rowSize; i++)
    {
      for (int j=0; j < columnSize; j++)
        matrix[i][j]= copy.matrix[i][j];
    }
}


Anything, if anything, would help!
const matrixType& operator=(const matrixType& right);

By convention, the overloaded assignment returns an non-const object reference. You will have trouble if you do this, for example

matrixType& mat = otherMat; //error trying to assign const reference to non-const reference

Better to change the signature to matrixType& operator=(const matrixType& right);

Since the overloaded operator returns a value, there is an error in your code within the operator itself.

1
2
3
4
5
6
7
8
9
10
const matrixType& matrixType::operator=(const matrixType &right)
{
  rowSize = right.rowSize;
  columnSize = right.columnSize;
  for (int i=0; i<rowSize; i++)
    {
      for (int j=0; j<columnSize; i++)
        matrix[i][j]= right.matrix[i][j];
    }
}


The above doesn't return a value. You should make it return *this; upon completion.

There's trouble here as well: matrixType m; because you have not defined the default constructor. Also, you haven't yet defined this constructor matrixType(int rows, int columns); although that doesn't cause errors in your current code.
I have implemented the default constructor, hopefully correctly.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
matrixType::matrixType()
{
  rowSize = 0;
  columnSize = 0;
}
matrixType::matrixType(int rows, int columns)
{
  rowSize=rows;
  columnSize=columns;
  for (int i=0; i<rowSize; i++)
    for (int j=0; j<columnSize; j++)
      matrix[i][j]=0;
}
void matrixType::setMatrix()
{
  cin >> rowSize >> columnSize;
}
istream& operator>>(istream& input, matrixType& m)
{
  for (int i=0; i < m.rowSize; i++)
    {
      for (int j=0; j<m.columnSize; j++)
        input >> m.matrix[i][j];
    }
  return input;
}
ostream& operator<<(ostream& output, const matrixType& m)
{
  for (int i=0; i< m.rowSize; i++)
    {
      for (int j=0; j<m.columnSize; j++)
        output << m.matrix[i][j] << " ";
      output << endl;
    }
  return output;
}
matrixType matrixType::operator+(matrixType right)
{
  matrixType m(right.rowSize, right.columnSize);
  m.rowSize = right.rowSize;
  m.columnSize = right.columnSize;
  for (int i=0; i < rowSize; i++)
    {
      for (int j=0; j< columnSize; j++)
        m.matrix[i][j]= matrix[i][j] + right.matrix[i][j];
    }
  return m;
}


I added the return *this statement and also removed the const as suggested.
I am getting a segmentation fault when I do the following in the client code:
m3=m1+m2;
cout << m3;



If I remove the for loop in the overloading operator= function, i get it to output a matrix full of zero's, but the right size.

:(
First, there's this:

~matrixType() { delete [] matrix; }

There's no need to delete [] the matrix array because it wasn't created dynamically (and this isn't the right way to do it if it were created dynamically with two dimensions).

There's also this:

1
2
3
4
5
6
7
8
9
10
11
12
matrixType& matrixType::operator=(const matrixType &right)
{
  rowSize = right.rowSize;
  columnSize = right.columnSize;
  for (int i=0; i<rowSize; i++)
    {
      for (int j=0; j<columnSize; i++)
        matrix[i][j]= right.matrix[i][j];
    }

   return *this;
}


In the j for loop, you're incrementing i, not j.
I have already fixed both of those problems. Something is still off though.
I'll figure it out eventually. Thank you!
Perhaps you could post the new code and a main() method reproducing the error, so that we can take a look as well.

I can run the following with the above changes without error:

1
2
3
4
5
6
7
8
9
10
11
12
13
int main()
{
     matrixType m1(50, 50);
     matrixType m2(50, 50);

     matrixType m3(30, 30);

     m3 = m1 + m2;

     cout<<m3;

     return 0;
}
I just finished rewriting and cleaning up the code, and everything works perfectly, as well as the main you ran. Who knows why the previous code didn't compile correctly for me. I do tend to look over small weird things, so it's not surprising.
Thank you for your help, shacktar! :)
Topic archived. No new replies allowed.