【问题标题】:Operator overloading errors for a simple 3x3 matrix class简单 3x3 矩阵类的运算符重载错误
【发布时间】:2019-06-23 05:52:23
【问题描述】:

所以我把它作为我的 class.cpp 文件

#include "matrixType3.h"
#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

void matrixType3::setElement(int row, int col, int newvalue)
{
    matrix[row][col] = newvalue;
}

double matrixType3::getElement(int row, int col)
{
    int j = matrix[row][col];
    return j;
}

void matrixType3::setMatrix(int x1, int x2, int x3, int x4, int x5, int x6, int x7, int x8, int x9)
{
    matrix[0][0] = x1;
    matrix[0][1] = x2;
    matrix[0][2] = x3;
    matrix[1][0] = x4;
    matrix[1][1] = x5;
    matrix[1][2] = x6;
    matrix[2][0] = x7;
    matrix[2][1] = x8;
    matrix[2][2] = x9;
}

void matrixType3::display()
{
    for (int i = 0; i < 3; i++)
    {
        for (int r = 0; r < 3; r++)
        {
            cout << matrix[i][r] << " ";
        }
        cout << endl;
    }
    cout << endl;
}

void matrixType3::displayRow(int row)
{
    for (int i = 0; i < 3; i++)
    {
        cout << matrix[row][i] << " ";
    }
    cout << endl;
}

void matrixType3::displayColumn(int col)
{
    for (int i = 0; i < 3; i++)
    {
        cout << matrix[i][col] << endl;
    }
}

void matrixType3::multipleMatrix(int c)
{
    for (int i = 0; i < 3; i++)
    {
        for (int r = 0; r < 3; r++)
        {
            int j = matrix[i][r];
            matrix[i][r] = j * c;
        }
    }
}

matrixType3 matrixType3::operator=(const matrixType3&mat) const
{
    matrixType3 tempMatrix;
    for (int i = 0; i < 3; i++)
    {
        for (int r = 0; r < 3; r++)
        {
            tempMatrix.matrix[i][r] = mat.matrix[i][r];
        }
    }
    return tempMatrix;
}

matrixType3 matrixType3::operator+(const matrixType3& mat) const
{
    matrixType3 tempMatrix;
    for (int i = 0; i < 3; i++)
    {
        for (int r = 0; r < 3; r++)
        {
            tempMatrix.matrix[i][r] = mat.matrix[i][r] + matrix[i][r];
        }
    }
    return tempMatrix;
}

matrixType3 matrixType3::operator-(const matrixType3& mat) const
{
    matrixType3 tempMatrix;
    for (int i = 0; i < 3; i++)
    {
        for (int r = 0; r < 3; r++)
        {
            tempMatrix.matrix[i][r] = matrix[i][r] - mat.matrix[i][r];
        }
    }
    return tempMatrix;
}

matrixType3 matrixType3::operator*(const matrixType3& mat) const
{
    matrixType3 tempMatrix;
    for (int i = 0; i < 3; i++)
    {
        for (int r = 0; r < 3; r++)
        {
            for (int k = 0; k < 3; k++)
            {
                tempMatrix.matrix[i][r] = tempMatrix.matrix[i][r] + matrix[i][k] * mat.matrix[k][r];
            }
        }
    }
    return tempMatrix;
}

matrixType3 matrixType3::operator*(const int&num) const
{
    matrixType3 tempMatrix;
    for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            tempMatrix.matrix[i][j] = matrix[i][j] * num;
        }
    }
    return tempMatrix;
}

bool matrixType3::operator==(const matrixType3& mat) const
{
    for (int i = 0; i < 3; i++)
    {
        for (int r = 0; r < 3; r++)
        {
            if (matrix[i][r] != mat.matrix[i][r])
                return false;
        }
    }
    return true;
}

matrixType3::matrixType3()
{
    for (int i = 0; i < 3; i++)
    {
        for (int c = 0; c < 3; c++)
        {
            matrix[i][c] = 0;
        }
    }
}

matrixType3::matrixType3(int x1, int x2, int x3, int x4, int x5, int x6, int x7, int x8, int x9)
{
    matrix[0][0] = x1;
    matrix[0][1] = x2;
    matrix[0][2] = x3;
    matrix[1][0] = x4;
    matrix[1][1] = x5;
    matrix[1][2] = x6;
    matrix[2][0] = x7;
    matrix[2][1] = x8;
    matrix[2][2] = x9;
}

matrixType3::~matrixType3()
{

}

但是,我的问题是运算符重载函数不起作用。 source.cpp 文件针对除所有运算符重载函数之外的所有其他成员函数运行。我想知道是否有人可以看到这段代码有什么问题?因为当我尝试 C = A * B 时,它只是在三乘三矩阵中返回构造的 0 的 c 矩阵。 该类只是应该采用 3x3 矩阵并对其进行编辑。 运算符重载用于 +、-、*、== 和 =。 作为旁注,编译器没有显示错误,但重载功能不起作用。

【问题讨论】:

  • 您的赋值运算符看起来很不寻常。通常的签名应该是matrixType3&amp; matrixType3::operator=(const matrixType3 &amp;other)。它不应该是 const 也不应该返回 const 引用。它应该将other 的值分配给*this 的成员并返回*this。 (第二种风格可能是移动赋值运算符。)

标签: c++ matrix operator-overloading


【解决方案1】:

不幸的是,OP 没有提供MCVE

因此,我复制/粘贴了部分 OP 代码,恕我直言 OK(而不是报告问题的原因),其余部分由我自己完成。

这是我准备的样本:

#include <iostream>

class matrixType3 {

  private:
    int matrix[3][3];

  public:
    matrixType3(): matrix{ 0 } { }

    matrixType3(int x1, int x2, int x3, int x4, int x5, int x6, int x7, int x8, int x9):
      matrix{ { x1, x4, x7 }, { x2, x5, x8 }, { x3, x6, x9 } }
    { }

    ~matrixType3() = default;

    matrixType3(const matrixType3&) = default;

#if 0 // OP's version:
    matrixType3 operator=(const matrixType3&mat) const
    {
      std::cout << "matrixType3 matrixType3::operator=(const matrixType3 &mat)()\n";
      matrixType3 tempMatrix;
      for (int i = 0; i < 3; i++) {
        for (int r = 0; r < 3; r++) {
          tempMatrix.matrix[i][r] = mat.matrix[i][r];
        }
      }
      return tempMatrix;
    }
#else // My version:
    /* This would be actually sufficient:
    matrixType3& operator=(const matrixType3&) = default;
     * but for demonstration a custom implementation:
     */
    matrixType3& operator=(const matrixType3 &mat)
    {
      std::cout << "matrixType3& operator=(const matrixType3 &mat)()\n";
      std::copy(
        std::begin(mat.matrix[0]), std::end(mat.matrix[0]),
        std::begin(matrix[0]));
      std::copy(
        std::begin(mat.matrix[1]), std::end(mat.matrix[1]),
        std::begin(matrix[1]));
      std::copy(
        std::begin(mat.matrix[2]), std::end(mat.matrix[2]),
        std::begin(matrix[2]));
      return *this;
    }
#endif // 0

    // copy/pasted from OP:
    matrixType3 operator+(const matrixType3& mat) const
    {
      matrixType3 tempMatrix;
      for (int i = 0; i < 3; i++) {
        for (int r = 0; r < 3; r++) {
          tempMatrix.matrix[i][r] = mat.matrix[i][r] + matrix[i][r];
        }
      }
      return tempMatrix;
    }

    /* a "getter" to provide read-only access to the member variable
     *
     * (Returning a const reference to a 2d array makes this function
     *a bit strange looking...) ;-)
     */
    const int (&get() const)[3][3] { return matrix; }
};

OP 声称

但是,我的问题是运算符重载函数不起作用。

我坚信 OP 的 operator=() 正是其他运营商看起来不错的问题。 OP 为operator= 选择的签名对我来说看起来很不寻常。 应该是:

matrixType3& operator=(const matrixType3 &mat)

操作员应将参数mat 分配给自己(即*this)。因此,它可能不是const,也不应该返回const 引用。 (后者可能会起作用,但会不必要地限制返回值的适用性。)

为了证明我走在正确的轨道上,我将 OP 的 operator=() 复制/粘贴到我的示例代码中,并准确观察了 OP 所描述的内容。

顺便说一句。我将输出语句放入赋值运算符中只是为了显示它何时被调用。

检查以上代码,其余示例:

std::ostream& operator << (std::ostream &out, const matrixType3 &mat)
{
  const int (&values)[3][3] = mat.get();
  return out
    << values[0][0] << '\t' << values[0][1] << '\t' << values[0][2] << '\n'
    << values[1][0] << '\t' << values[1][1] << '\t' << values[1][2] << '\n'
    << values[2][0] << '\t' << values[2][1] << '\t' << values[2][2] << '\n';
}

int main()
{
  matrixType3 mat1(11, 21, 31, 12, 22, 32, 13, 23, 33);
  std::cout << "mat1:\n" << mat1;
  matrixType3 mat2;
  std::cout << "mat2:\n" << mat2;
  std::cout << "mat2 = mat1;\n";
  mat2 = mat1;
  std::cout << "mat2:\n" << mat2;
  std::cout << "mat1 + mat2:\n" << mat1 + mat2;
  matrixType3 mat3;
  mat3 = mat1 + mat2;
  std::cout << "mat3 = mat1 + mat2:\n" << mat3;
  // This is no assigment:
  std::cout << "No assignment: matrixType3 mat4 = mat1;\n";
  matrixType3 mat4 = mat1;
  std::cout << "... but initialization/copy construction\n";
  std::cout << "mat4:\n" << mat4;  
  return 0;
}

编译和测试(使用我的operator=()):

mat1:
11  12  13
21  22  23
31  32  33
mat2:
0   0   0
0   0   0
0   0   0
mat2 = mat1;
matrixType3& operator=(const matrixType3 &mat)()
mat2:
11  12  13
21  22  23
31  32  33
mat1 + mat2:
22  24  26
42  44  46
62  64  66
matrixType3& operator=(const matrixType3 &mat)()
mat3 = mat1 + mat2:
22  24  26
42  44  46
62  64  66
No assignment: matrixType3 mat4 = mat1;
... but initialization/copy construction
mat4:
11  12  13
21  22  23
31  32  33

Live Demo on coliru

在准备示例代码时,我偶然发现:

matrixType3 mat1;
matrixType3 mat2; mat2 = mat1;

对比

matrixType3 mat1;
matrixType3 mat2 = mat1;

我觉得也值得详细说明。

虽然两个样本看起来非常相似,但有很大的不同: 1st 使用复制赋值,但在 2nd 中,= 被编译为初始化(因为直接遵循声明)。因此,在第 2nd 的情况下,没有评估。 (而是调用了复制构造函数。)

【讨论】:

    猜你喜欢
    • 2012-05-04
    • 1970-01-01
    • 2015-07-29
    • 1970-01-01
    • 1970-01-01
    • 2016-02-16
    • 1970-01-01
    • 1970-01-01
    • 2016-04-14
    相关资源
    最近更新 更多