我的矩阵乘法算法有什么问题,求解答


#include<iostream>
#include<string.h>
#include<ostream>
using namespace std;
class Matrix {
private:
    int row;
    int col;
    int size;
    double* data;

public:
    Matrix(int r, int c) {
        row = r;
        col = c;
        size = row * col;
        data = new double[size];
        cout << "请输入" << row << "*" << col << "矩阵:" << endl;
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                cin >> data[i * row + j];
            }
        }
    }
    ~Matrix(void) {
        delete[]data;
    }
    Matrix(const Matrix& M)
    {
        col = M.col;
        row = M.row;
        size = M.size;
        data = new double[M.size];
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                data[i * row + j] = M.data[i * row + j];
            }
        }

    }
    Matrix& operator=(Matrix& M)
    {
        if (this == &M)
        {
            return *this;
        }
        col = M.col;
        row = M.row;
        size = M.size;
        data = new double[M.size];
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                data[i * row + j] = M.data[i * row + j];
            }
        }
        return *this;
    }
    Matrix operator+(Matrix& M) {
        Matrix M1 = *this;
        if (col == M.col && row == M.row) {
            for (int i = 0; i < M1.row; i++) {
                for (int j = 0; j < M1.col; j++) {
                    M1.data[i * row + j] = data[i * row + j] + M.data[i * row + j];
                }
            }
            return M1;
        }
        else {
            cout << "矩阵行或列数不相等,不能相加" << endl;
            return M1;
        }
    }
    Matrix operator*(Matrix& M)
    {
        int x;
        Matrix M1 = *this;
        cout << "请选择你要进行的运算:1.点乘     2.乘法: ";
        cin >> x;
        if (x == 1)
        {
            if (col == M.col && row == M.row) {
                for (int i = 0; i < M1.row; i++) {
                    for (int j = 0; j < M1.col; j++) {
                        M1.data[i * row + j] = data[i * row + j] * M.data[i * row + j];
                    }
                }
                return M1;
            }
            else {
                cout << "矩阵行或列数不相等,不能相乘" << endl;
                return M1;
            }
        }
        else if (x == 2)
        {
            if (row == M.col && col == M.row) {
                for (int i = 0; i < row; i++) {
                    for (int j = 0; j <  row; j++) {
                        int sum = 0;
                        for (int k = 0; k < row; k++) {
                            sum += data[i * row + k] * M.data[j * col + k];
                        }
                        M1.data[i * row + j] = sum;
                    }
                    return M1;
                }
            }
            else {
                cout << "矩阵不能相乘" << endl;
                return M1;
            }
        }
    }

    friend ostream& operator<<(ostream& out, Matrix& M)
    {
        for (int i = 0; i < M.row; i++) {
            for (int j = 0; j < M.col; j++) {
                out << M.data[i * M.row + j] << ' ';
            }
            out << endl;
        }
        return out;
    }
    friend Matrix operator+(Matrix& M, int val)
    {
        for (int i = 0; i < M.row; i++) {
            for (int j = 0; j < M.col; j++) {
                M.data[i * (M.row) + j] += val;
            }
        }
        return M;
    }

    friend Matrix operator*(Matrix& M, int val)
    {
        for (int i = 0; i < M.row; i++) {
            for (int j = 0; j < M.col; j++) {
                M.data[i * (M.row) + j] *= val;
            }
        }
        return M;
    }
};

    int main(void) {
        Matrix M1(2, 2);
        Matrix M2(2, 2);
        Matrix M3 = M1;
        Matrix M4 = M1 + M2;
        Matrix M6 = M3 + 2;
        Matrix M7 = M1 * M2;
        Matrix M10 = M3 * 2;
        cout << "M1:" << endl << M1 << endl;
        cout << "M2:" << endl << M2 << endl;
        cout << "M3 = M1:" << endl << M3 << endl;
        cout << "M4 = M1 + M2:" << endl << M4 << endl;
        cout << "M1+2= " << endl << M6 << endl;
        cout << "M7 = M1 * M2:" << endl << M7 << endl;
        cout << "M1*2= " << endl << M10 << endl;
        system("pause");
        return 0;
    }

img

img