Matrix addition not working

Feb 6, 2017 at 3:55am
So I was given a header file from my professor that contained a class declaration that builds a matrix, as well as can alter a few things about it. Anyway I decided I wanted to add some more functions to the class and play with pointers a bit since I don't have much experience with them, but I'm getting some odd errors

Matrix Class
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
  class mat
{
public:
    mat(){A=NULL;}
    ~mat(){}


    mat(int r, int c){row=r; col=c;         ///Build an empty matrix of row r and column c
                    A=new double *[row];
                    for(int i=0; i<row; i++)
                    {
                        A[i]=new double[col];
                    }
                    for(int i=0; i<row; i++)
                    {
                        for(int j=0; j<col; j++)
                        {
                            A[i][j]=0.0;
                        }
                    }
            }


            int getrow(){return row;}
            int getcol(){return col;}     ///Retrieve the needed info

/// insert other functions that are not used in the addition

private:
    int row;
    int col;
    double ** A;
};
 


That all is what the prof provided me before, now here's what I've added to it,
(also note for some reason I'm having trouble getting this->mat[i][j] to work so I'm trying to get the basic math to work before worrying about efficiency.)

Matrix addition
1
2
3
4
5
6
7
8
9
10
11
12
13
14
mat matAdd(mat a, mat b) ///add two matrices to each other
    {
        int tempA = b.getrow();
        int tempB = b.getcol();
        mat c(tempA,tempB);
           for(int i=0; i<tempA; i++)
            {
                for(int j=0; j<tempB; j++)
                {
                  c[i][j] = a[i][j]+b[i][j];
                }
            }
            return c;
        }


The errors I'm getting are three instances of error: no match for ‘operator[]’ (operand types are ‘mat’ and ‘int’) all aimed at the line
c[i][j] = a[i][j]+b[i][j];

Any help would be appreciated because I also want to work in subtraction, multiplication, and maybe a sorting function before the end of the night, and this little road block effects most of the others similarly I imagine. Thank you.
Last edited on Feb 6, 2017 at 3:56am
Feb 6, 2017 at 4:07am
mat does not support operator [] for direct access. You can however access array members manually.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
mat matAdd(mat a, mat b)
    {
        int tempA = b.getrow();
        int tempB = b.getcol();
        mat c(tempA,tempB);
           for(int i=0; i<tempA; i++)
            {
                for(int j=0; j<tempB; j++)
                {
                  c.A[i][j] = a.A[i][j] + b.A[i][j]; // <==
                }
            }
            return c;
        }
Feb 6, 2017 at 4:14am
Ah that fixed the problem immediately! Now both matAdd and matSub work like a charm.
Feb 6, 2017 at 4:59am
I highly recommend you do 1 dimensional arrays for your matrix library if you decide to go into this any deeper. Its a little aggravating, but it pays off royally when you get into advanced linear algebra and need to resize or reshape a matrix. For example, a simple transpose of a 2x4 into a 4x2 is much, much easier with 1d, as you can just swap the row and col size values and flip specific entries across the old diagonal. There are many other places where this is useful, such as appending a solution column before doing a RREF solver or appending an identity matrix to crank out an inverse. It may be C, but re-alloc is your friend, or increasing a valarray's memory if you go that route.

I would also say that this is an EXCELLENT place to over load your operators.
you can literally write a = b+c; and have it work that way, and its much cleaner than a = add(b,c);

Just throwing some stuff out there if you want to dig deeper. What you have is great learning and practice. It gets cool when you start peeling off the eigenvalues or dealing with condition numbers / numerical instability.





Topic archived. No new replies allowed.