Need help on multiply matrix operator.

I am trying to overload Multiplication Matrix operator with the following but I can't:
This is the .cpp
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
CFMatrix::CFMatrix(int m,int n)
{
    _rowCount = m;
    _columnCount = n;
    clear();
}

CFMatrix CFMatrix::operator+(CFMatrix that)
{
    CFMatrix answer(this->rowCount(),this->columnCount());
    for(int i=0;i<this->rowCount();i++)
    {
        for(int j=0;j<this->columnCount();j++)
        {
            answer.setItem(i,j,this->item(i,j)+that.item(i,j));
        }
    }
    return answer;
}

double CFMatrix::item(int m, int n)
{
    return _body[m][n];
}

void CFMatrix::setItem(int m, int n, double value)
{
    _body[m][n]=value;
}

int CFMatrix::rowCount()
{
    return _rowCount;
}

int CFMatrix::columnCount()
{
    return _columnCount;
}

void CFMatrix::clear()
{
    for(int i=0;i<rowCount();i++)
    {
        for(int j=0;j<columnCount();j++)
        {
            _body[i][j]=0.0;
        }
    }
}
//this is operator I have issue with.
CFMatrix CFMatrix::operator *(CFMatrix that)
{ 
   if(this->rowCount()>this->columnCount())
   {
       CFMatrix answer(this->rowCount()-this->columnCount(),that.columnCount());
   }
    else
   {
       CFMatrix answer(this->columnCount()-this->rowCount(),that.columnCount());
   }

if(this->rowCount()==that.columnCount() && this->columnCount()==that.rowCount())
	{
            for(int i=0;i<this->rowCount();i++)
            {
                for(int j=0;j<this->columnCount();j++)
		{
                        answer.setItem(i,j,this->item(i,j)*that.item(j,i));
                }
//How do you add them up????
            }
        }
    return answer;
}


And the following is the .h
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

class CFMatrix {
  public:
    static const int MAX_ROWS = 8, MAX_COLS = 8;
  private:
    double _body[MAX_ROWS][MAX_COLS];
    int _rowCount, _columnCount;
  public:
    CFMatrix(int m, int n);
    // Post: A new m X n matrix is created with all values initialized to 0.0

    void clear();
    // Post: This matrix is reset to all zeros

    int rowCount();
    // Post: The number of rows in this matrix is returned

    int columnCount();
    // Post: The number of columns in this matrix is returned

    double item(int m, int n);
    // Pre: 0 <= m < rowCount()
    // Pre: 0 <= n < columnCount()
    // Post: the value stored at row m, column n is returned

    void setItem(int m, int n, double value);
    // Pre: 0 <= m < rowCount()
    // Pre: 0 <= n < columnCount()
    // Post: value is stored at row m, column n

    CFMatrix operator +(CFMatrix that);
    // Pre: this.rowCount() == that.rowCount()
    // Pre: this.columnCount() == that.columnCount()
    // Post: this and that are added together, and the sum matrix is returned

    CFMatrix operator -(CFMatrix that);
    // Pre: this.rowCount() == that.rowCount()
    // Pre: this.columnCount() == that.columnCount()
    // Post: that is subtracted from this, and the difference matrix is returned

    CFMatrix operator *(double scalar);
    // Post: The result of the scalar multiplication is returned

    CFMatrix operator *(CFMatrix that);
    // Pre: this.columnCount() == that.rowCount()
    // Post: this is multiplied by that, and the product matrix is returned
};

Any 1 have any idea or hints would be great! I am very confuse now...><Ty
Your operators should have the form

CFMatrix operator * (const CFMatrix & that) const;
1
2
3
4
5
6
7
8
9
10
CFMatrix CFMatrix::operator * (const CFMatrix & that) const
{
  if (this->columnCount() != that.rowCount()) fooey();

  CFMatrix answer( this->rowCount(), that.columnCount() );

  ...

  return answer;
}

You cannot declare answer in two different places. Declare it once at the top, and mess with it later (as I have in the example above).

Matrix multiplication is relatively straightforward, but you must pay attention. See here for help:
http://www.mathwarehouse.com/algebra/matrix/multiply-matrix.php

Hope this helps.
Thank you Duoas!
But I still don't get why the following is illegal(Error:invalid use of 'void') something like this, which make sense to me and this should be the way to do it, cannot think of any other:
 
answer.setItem(i,j,this->item(i,j))+=answer.setItem(i,j,this->item(i,k)*that.item(k,j));

don't worry about the k it is just another loop I made, but I was thinking about is it possible to do it like this? like an array?
Last edited on
You are using += in the line of code, and setItem returns a void. So, you are trying to assign to something that doesn't return a type. In general, you can't assign to function results either unless you are returning a reference.
Topic archived. No new replies allowed.