繁体   English   中英

在矩阵模板类中重载运算符 *= 和运算符 +

[英]Overloading Operator *= and Operator+ in Matrix Template Class

程序matrix.cc的方法template <class T> const Matrix<T> &Matrix<T>::operator*=(T rhs)应该能够返回由 rhs 缩放的矩阵的调用对象,并且参数 rhs 将是与矩阵相同的类型。

我从编译器收到的错误输出是:

[hw7] make clean && make bin/test_matrix_mul_assign && ./bin/test_matrix_mul_assign
rm -f bin/*
g++   -std=c++11   -Wall       -I inc   -I src   -c   src/test_matrix_mul_assign.cc -o bin/test_matrix_mul_assign.o
g++     bin/test_matrix_mul_assign.o   -o bin/test_matrix_mul_assign
Testing Matrix::operator*=
  Expected Matrix[0][0]: 2.0, Actual: 1
  FAILED

当我知道我通过了// TEST MUL ASSIGMENT OP CORRECT RETURN部分时,有人能告诉我为什么我会收到这个“失败”的输出。

这是我的 matrix.cc:

#include <matrix.h>

template <class T>
const Matrix<T> &Matrix<T>::operator*=(T rhs) {
  unsigned int rows_ = 0;
  unsigned int cols_ = 0;
  T **m_ = nullptr;

  for (unsigned int i = 0; i < rows_; ++i) {
    m_[i] = new T[cols_];
    for (unsigned int j = 0; j < cols_; ++j) {
      m_[i][j] = m_[i][j] * rhs;
    }
  }
  return *this;
}

template <class T>
const Matrix<T> Matrix<T>::operator+(const Matrix<T> &rhs) const {
  if (!(this->cols_ == rhs.cols_) && (this->rows_ == rhs.rows_)) {
    std::cout << "Cannont add matrices. Wrong dimensions\n";
    exit(0);
  }

  Matrix<T> lhs;
  lhs.rows_ = this->rows_;
  lhs.cols_ = this->cols_;

  for (unsigned int i = 0; i < lhs.rows; ++i) {
    for (unsigned int j = 0; j < lhs.cols_; ++j) {
      lhs[i] += rhs[i];
    }
  }
  return lhs;
}

这是我的 matrix.h:

#include <cassert>
// using assert
#include <exception>
#include <iostream>

template <class T>
class Matrix {
 public:
  friend class MatrixTester;

  /* Times Equals Op: 1 Point
   *   Returns calling object with matrix scaled by rhs.
   *   Parameter:
   *    - rhs will be the same type as the matrix
   */
  const Matrix<T> &operator*=(T rhs);

  /* Add Op: 1 Point
   *   Returns the sum of calling object's matrix and rhs's matrix as a new
   *   object.
   * Precondition(s):
   *   - lhs rows must equal rhs rows
   *   - lhs cols must equal rhs cols
   */
  const Matrix<T> operator+(const Matrix<T> &rhs) const;

 private:
  T **m_;
  unsigned int rows_;
  unsigned int cols_;
};

#include <matrix.cc>  //NOLINT

这是我的 test_matrix_mul_assign.cc 测试仪:

#include <test_matrix.h>

int main(int argc, char **argv) {
  MatrixTester tester;

  cout << "Testing Matrix::operator*=" << endl;
  if (tester.Test_MulAssignOp()) {
    cout << "  PASSED" << endl;
    return 0;
  }

  cout << "  FAILED" << endl;
  return 1;
}

bool MatrixTester::Test_MulAssignOp() const {
  const int kRows = 3, kCols = 4;
  Matrix<double> test_m;
  test_m.m_ = new double *[kRows];
  for (unsigned int i = 0; i < kRows; ++i) {
    test_m.m_[i] = new double[kCols];

    for (unsigned int j = 0; j < kCols; ++j)
      test_m.m_[i][j] = (i + 1.0) * (j + 1.0);
  }
  test_m.rows_ = kRows;
  test_m.cols_ = kCols;

  // TEST MUL ASSIGMENT OP CORRECT RETURN
  const Matrix<double> *m_ptr = &(test_m *= 2.0);
  if (m_ptr != &test_m) {
    cout << "  Expected return address of assigment: " << &test_m
         << ", Actual: " << m_ptr << endl;
    return false;
  }
  // TEST MUL ASSIGMENT OP CALCULATION
  if (test_m.m_[0][0] != 2.0) {
    cout << "  Expected Matrix[0][0]: 2.0, Actual: " << test_m.m_[0][0] << endl;
    return false;
  }
  if (test_m.m_[1][3] != 16.0) {
    cout << "  Expected Matrix[1][3]: 16.0, Actual: " << test_m.m_[1][3]
         << endl;
    return false;
  }
  if (test_m.m_[2][2] != 18.0) {
    cout << "  Expected Matrix[2][2]: 18.0, Actual: " << test_m.m_[2][2]
         << endl;
    return false;
  }
  return true;
}

operator*=实现中有以下变量定义:

unsigned int rows_ = 0;
unsigned int cols_ = 0;
T **m_ = nullptr;

他们将影响班级的所有成员。 当您稍后在函数定义中使用rows_cols_m_ ,它们将引用这些局部变量,而不是当前实例的类成员。

因此,您对operator*=有效实现根本什么都不做。 对正确值的测试随后失败也就不足为奇了。

只需删除这些声明,以便名称将引用当前实例的成员。

然后也不会有任何指向m_[i] = new T[cols_]; . 所以也删除它。


为什么所有这些动态分配首先都带有new 这是非常糟糕的风格,会给你带来各种各样的问题。 改用std::vector


class Matrix似乎也缺乏适当的构造函数。 这又会导致各种问题,尤其是当它把内存分配留给类的用户时。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM