0

I'm trying to multiply 2 matrices together using operator-overloading but I'm getting an error when trying to convert my int array into my matrix class in order to use it. I'm not sure how to fix these errors

#include <iostream>
using namespace std;

struct matrix{
    int array[4][4];
public:
    void make(int A[][4], int size) {
        for (int x = 0; x < size; x++) {
            for (int y = 0; y < size; y++) {
                array[x][y] = A[x][y];
            }
        }
    }
    void operator*(matrix m) {
        int output[4][4];
        for (int x = 0; x < 4; x++){
            for (int y = 0; y < 4; y++){
                array[x][y] = (array[x][y] * m.array[x][y]);
            }
        }

        for (int x = 0; x < 4; x++) {
            for (int y = 0; y < 4; y++) {
                cout << output[x][y] << " ";
            }
            cout << endl;
        }
    }
};

int main(){
    matrix m1, m2;
    int a[4][4] = {{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16}};
    //int   c[3][3] = {{1,2,3},{4,5,6},{7,8,9}};
    m1.make(a[4][4], 4);
    m2.make(a[4][4], 4);
    m1*m2;

    return 0;
}

2 Answers 2

1

There are two problem with your code:-

m1.make(a[4][4], 4); // should be m1.make(a, 4); make is expecting a 2D array you are passing an integer

In the operator *function

array[x][y] = (array[x][y] * m.array[x][y]); // your should assign to output[x][y] here
Sign up to request clarification or add additional context in comments.

Comments

0

You are not using the corect syntax.

int main(){
    matrix m1, m2;
    int a[4][4] = {{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16}};
    //int   c[3][3] = {{1,2,3},{4,5,6},{7,8,9}};

    // these are not good, you try to pass a single int, and it is past the
    // end of a.
    //m1.make(a[4][4], 4);
    //m2.make(a[4][4], 4);

    // try this instead: 
    m1.make(a, 4);
    m2.make(a, 4);

    m1*m2;

    return 0;
}

Multiplying is a binary operator that takes in 2 matrices, and outputs a third one with the result.

class matrix
{
    // note the friend modifier, which indicates this fonction is a not a member
    // of the class, and can access all of its private members.   

   friend matrix operator*(const matrix& m, const matrix& n) 
    {
        matrix result;
    
        for (int x = 0; x < 4; x++)
            for (int y = 0; y < 4; y++)
                result.array[x][y] = (m.array[x][y] * n.array[x][y]);

        // your formula does not seem quite right here...
        // I'll let you fix it.
        
        return result;
    }

Here is the correct syntax for make() to accept an int[4][4] array:

void make(int (&A)[4][4]) {
    for (int x = 0; x < 4; x++) {
        for (int y = 0; y < 4; y++) {
            array[x][y] = A[x][y];
        }
    }
}

Why don't you have a constructor instead of make?

class matrix{
  private:
    int array[4][4];
public:
    // you want to initialize your data, always (well, almost always, but
    // this is the kind of data that always applies to)...
    matrix()
    {
        for (int x = 0; x < 4; x++)
            for (int y = 0; y < 4; y++)
                array[x][y] = 0;
    }

    explicit matrix(const int(&a)[4][4])
    {
        for (int x = 0; x < 4; x++)
            for (int y = 0; y < 4; y++)
                array[x][y] = a[x][y];
    }

    matrix& operator=(const int(&a)[4][4])
    {
        return *this = matrix(a);
    }

    matrix& operator=(const matrix& m)
    {
        for (int x = 0; x < 4; x++)
            for (int y = 0; y < 4; y++)
                array[x][y] = m.array[x][y];
        return *this;
    }

    // ...
};

Then main() becomes:

int main(){
    matrix m1, m2;
    int a[4][4] = {{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16}};

    matrix m1(a);
    matrix m2(a); 

    matrix product = m1 * m2;

    return 0;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.