将 int 数组转换为矩阵 class 以完成运算符重载

Issue turning int array into matrix class to complete operator-overloading

我正在尝试使用运算符重载将 2 个矩阵相乘,但在尝试将我的 int 数组转换为我的矩阵 class 以便使用它时出现错误。我不确定如何修复这些错误

#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;
}

你的代码有两个问题:-

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

operator *函数中

array[x][y] = (array[x][y] * m.array[x][y]); // your should assign to output[x][y] here

您没有使用正确的语法。

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;
}

乘法是一个二元运算符,它接受 2 个矩阵,并输出第三个矩阵和结果。

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;
    }

下面是 make() 接受 int[4][4] 数组的正确语法:

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];
        }
    }
}

为什么不用构造函数而不是 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;
    }

    // ...
};

然后 main() 变成:

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;
}