在一个命令中使用2个重载运算符会导致错误

Tal*_*sky 1 c++ class operator-overloading

我在C++中创建了一个带有重载运算符的矩阵类,我重载了运算符,<<(输出,使用ostream库),运算符+添加到矩阵,运算符=用于将一个矩阵分配给另一个矩阵.问题是,当我使用时

cout<<m1+m2<<endl;
Run Code Online (Sandbox Code Playgroud)

我收到一个错误

E0349: No operator << matches these operands type are: std::ostream << Matrix
Run Code Online (Sandbox Code Playgroud)

但如果我做以下事情:

Matrix m = m1 + m2;
cout<<m<<endl;
Run Code Online (Sandbox Code Playgroud)

它完美无缺.这是<<运算符:

ostream& operator<< (ostream &os,const Matrix& m)
{
    if (m.isValid())
    {
        os << '|';
        for (int i = 0; i < m.getRows(); i++)
        {
            for (int j = 0; j < m.getCols(); j++)
            {
                os << m.getMatrix()[i][j];
                if (j < m.getCols() - 1)
                {
                    os << ',';
                }
            }
            os << '|';
        }
    }
    else
    {
        os << "invalid matrix!";
    }
    return os;
}
Run Code Online (Sandbox Code Playgroud)

+运算符:

Matrix Matrix::operator+ (Matrix &m)
{
    Matrix* answer = new Matrix(m); //allocating space
    if (valid && m.valid)//if they are both valid
    {
        if (colNum == m.colNum&&rowNum == m.rowNum) //if are from same size
        {
            answer->valid = true; //valid set to be true
            for (int i = 0; i < rowNum; i++) //going over the matrix
            {
                for (int j = 0; j < colNum; j++)
                {
                    answer->matrix[i][j] += matrix[i][j]; //adding data
                }
            }
        }
        else
        {
            //clearing data
            delete answer;
            answer = new Matrix();
        }
    }
    else
    {
        //clearing data
        delete answer;
        answer = new Matrix();
    }
    return *answer;
}
Run Code Online (Sandbox Code Playgroud)

和=运算符:

Matrix Matrix::operator= (Matrix &m)
{
    int rows = m.rowNum; //putting cols and rows from the data
    int cols = m.colNum;
    if (m.valid)
    {
        matrix = new int*[rows]; //defining the matrix - first allocatin space for rows
        for (int i = 0; i < rows; i++) //now allocating space for cols
        {
            matrix[i] = new int[cols];
        }
        for (int i = 0; i < rows; i++) //now going over the matrix and putting the data in
        {
            for (int j = 0; j < cols; j++)
            {
                matrix[i][j] = m.matrix[i][j];
            }
        }
    }
    //putting the rows and cols data
    rowNum = m.rowNum;
    colNum = m.colNum;
    valid = m.valid; //setting to the right valid type
    return *this;
}
Run Code Online (Sandbox Code Playgroud)

和类变量:

class Matrix
{
private:
    bool valid;
    int** matrix;
    int rowNum;
    int colNum;
Run Code Online (Sandbox Code Playgroud)

复制构造函数是一个字符串构造函数和一个构造函数,它根据算法获取字符串输入并将其转换为矩阵.

ein*_*ica 7

您实际上面临的事实是您无法传递临时对象作为参考(链接是在StackOverflow上的另一个问题).

您的案例中的解决方案应该是替换:

ostream& operator<< (ostream &os,Matrix& m)
Run Code Online (Sandbox Code Playgroud)

有:

ostream& operator<< (ostream &os,const Matrix& m)
Run Code Online (Sandbox Code Playgroud)

作为重载的运算符签名.您目前需要一个左值引用,在大小写的情况下

Matrix m = m1 + m2;
cout << m << endl;
Run Code Online (Sandbox Code Playgroud)

这就是你用函数调用函数m- 一个命名变量,并且将比命令的执行寿命更长.在失败的情况下,您正在调用函数m1+m2- 加号操作的结果,这是一个临时对象.它只是与你的超载不匹配.

顺便说一下 - 这种变化也是有道理的,因为你在打印时没有修改矩阵 - 你把它视为const.