运算符为 Array 重载但不在 Main 中工作

Operator Overloaded for Array but not working in Main

我写了一个程序来对两个矩阵进行加、减和乘。为此,我重载了运算符 +、- 和 *,但是当我在 main 中使用它们时,我收到一条错误消息:

no operator "+" matches these operands我想不通。也许我使用了不正确的逻辑来重载 class 的运算符,现在我坚持使用它。 代码如下

#include <iostream>
#include <conio.h>
#include <string>
#include <ctime>

using namespace std;

class ERROR
{
public:
    string E = "Number of Columns of Matrix A is not Equal to Number of Rows of Matrix B";
};
class MATRIX
{
private:
    static const int number = 100;
    int matrix[number][number];
    int n; 
    int m;
public:
    MATRIX();
    MATRIX(int,int);
    void display();
    MATRIX operator+(MATRIX x[]);
    MATRIX operator-(MATRIX x[]);
    MATRIX operator*(MATRIX x[]);
    MATRIX operator=(MATRIX x[]);
};

int main()
{
    srand(time(0));
    MATRIX x(2, 2), y(2, 2);
    cout << "Matrix 1 " << endl;
    x.display();
    cout << endl;
    cout << "Matrix 2 " << endl;
    y.display();
    cout << endl;
    MATRIX z(2, 2);
    z = x + y;
}


// Constructors of Class Matrix

MATRIX::MATRIX()
{
    n = number; m = number;
    for (int loop = 0; loop < number; loop++)
    {
        for (int loop2 = 0; loop2 < number; loop2++)
        {
            matrix[loop][loop2] = 1 + rand() % 50;
        }
    }
}

MATRIX::MATRIX(int rows, int col)
{
    n = rows; m = col;
    for (int loop = 0; loop < n; loop++)
    {
        for (int loop2 = 0; loop2 < m; loop2++)
        {
            matrix[loop][loop2] = 1 + rand() % 50;
        }
    }
}

// Display function for Class

void MATRIX::display()
{
    for (int loop = 0; loop < n; loop++)
    {
        for (int loop2 = 0; loop2 < m; loop2++)
        {
            cout << matrix[loop][loop2] << " ";
        }
        cout << endl;
    }
    cout << endl;
}

// Operators Overloaded for Array

MATRIX MATRIX::operator+(MATRIX x[])
{
    MATRIX z(n,m);
    for (int loop = 0; loop < n; loop++)
    {
        for (int loop2 = 0; loop2 < m; loop2++)
        {
            z.matrix[loop][loop2]= matrix[loop][loop2] + x->matrix[loop][loop2];
        }
    }
    return z;
}
MATRIX MATRIX::operator-(MATRIX* x)
{
    MATRIX z(n, m);
    for (int loop = 0; loop < n; loop++)
    {
        for (int loop2 = 0; loop2 < m; loop2++)
        {
            z.matrix[loop][loop2] = matrix[loop][loop2] - x->matrix[loop][loop2];
        }
    }
    return z;
}
MATRIX MATRIX::operator*(MATRIX x[])
{
    try
    {
        if (m == x->n)
        {
            MATRIX *y[number][number];
            for (int loop = 0; loop < n; loop++)
            {
                for (int loop2 = 0; loop2 < m; loop2++)
                {
                    for (int loop3 = 0; loop3 < m; loop3++)
                    {
                        y[loop][loop2] += matrix[loop][loop3] * x->matrix[loop3][loop2];
                    }
                }
            }
            return *this;
        }
        throw;
    }
    catch (ERROR e)
    {
        cout << e.E << endl;
        _getch();
        exit(1);
    }
}

MATRIX MATRIX::operator=(MATRIX x[])
{
    for (int loop = 0; loop < n; loop++)
    {
        for (int loop2 = 0; loop2 < m; loop2++)
        {
            matrix[loop][loop2] = x->matrix[loop][loop2];
        }
    }
    return *this;
}


您已经在 MATRIXMATRIX[] 之间定义了运算符 +(即 MATRIX 的数组)。您应该修改定义以对 MATRIX 和另一个 MATRIX:

进行操作
MATRIX operator+(MATRIX x);

当然,相应地修改实施。

编辑:
正如 Fabien 在评论中提到的,使用 const 引用将在使用此运算符时节省复制第二个操作数:

MATRIX operator+(const MATRIX& x);