【问题标题】:Using 2 overloaded operators in one command cause an error在一个命令中使用 2 个重载运算符会导致错误
【发布时间】:2018-05-24 11:49:59
【问题描述】:

我在 C++ 中创建了一个带有重载运算符的矩阵类,我重载了运算符,

cout<<m1+m2<<endl;

我收到一个错误

E0349: No operator << matches these operands type are: std::ostream << Matrix

但如果我执行以下操作:

Matrix m = m1 + m2;
cout<<m<<endl;

效果很好。 这是

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

+ 运算符:

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

还有 = 运算符:

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

和类变量:

class Matrix
{
private:
    bool valid;
    int** matrix;
    int rowNum;
    int colNum;

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

【问题讨论】:

  • 与您的问题无关,但您的 operator+ 函数存在内存泄漏。
  • 与您的问题相关的更多信息,我建议您阅读例如this canonical implementation reference。我建议这样做,因为一方面,您的复制分配运算符并不完全正确。此外,如果您仔细阅读链接参考,您将看到 operator&lt;&lt; 函数应该采用哪些参数。提示:非常量引用不能绑定到非左值。

标签: c++ class operator-overloading


【解决方案1】:

您实际上面临着Can't pass temporary object as reference 的事实(该链接指向 StackOverflow 上的另一个问题)。

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

ostream& operator<< (ostream &os,Matrix& m)

与:

ostream& operator<< (ostream &os,const Matrix& m)

作为重载的操作符签名。您目前需要一个左值引用,如果是

Matrix m = m1 + m2;
cout << m << endl;

这就是您使用 m 调用函数的内容 - 一个命名变量,并且会比命令的执行时间更长。在失败的情况下,您使用m1+m2 调用该函数 - 加号操作的结果,它是一个临时对象。它只是与您的过载不匹配。

顺便说一句 - 此更改也很有意义,因为您在打印时并未修改矩阵 - 您将其视为 const

【讨论】:

  • @TalSokolinsky:好的,所以让我们删除答案中的所有 cmets。
猜你喜欢
  • 2020-06-09
  • 1970-01-01
  • 2021-02-01
  • 1970-01-01
  • 2011-08-12
  • 2022-01-16
  • 2021-07-17
  • 1970-01-01
  • 2023-03-11
相关资源
最近更新 更多