簡體   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