Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/160.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 将int数组转换为矩阵类以完成运算符重载的问题_C++_Matrix_Operator Overloading - Fatal编程技术网

C++ 将int数组转换为矩阵类以完成运算符重载的问题

C++ 将int数组转换为矩阵类以完成运算符重载的问题,c++,matrix,operator-overloading,C++,Matrix,Operator Overloading,我试图使用运算符重载将两个矩阵相乘,但在尝试将int数组转换为matrix类以使用它时出错。我不知道如何修复这些错误 #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数组转换为matrix类以使用它时出错。我不知道如何修复这些错误

#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;
}
#包括
使用名称空间std;
结构矩阵{
整数数组[4][4];
公众:
无效生成(整数A[][4],整数大小){
用于(int x=0;xcout您的代码有两个问题:-

m1.make(a[4][4],4);//应该是m1.make(a,4);make需要一个传递整数的二维数组
运算符*
函数中

array[x][y]=(array[x][y]*m.array[x][y]);//您应该在此处分配到输出[x][y]

您没有使用corect语法

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;
}
乘法是一种二进制运算符,它接受两个矩阵,并输出第三个矩阵和结果

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

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