【问题标题】:Matrix template addition operator overloading矩阵模板加法运算符重载
【发布时间】:2016-02-28 17:48:09
【问题描述】:

我的任务是编写一个具有一些特定功能的模板 Matrix 类。我认为operator=copy constructorparameter constructor 工作正常。 operator+ 有问题。例如,当我添加 2 个仅填充数字 5 的矩阵时,我得到了整个 row 1 和整个 column m 的奇怪数字,最后一行没有元素。类似的东西(o - 好的,x - 错误的结果)。

xxxx
ooox
ooox
oooo

头文件

template <typename T> class Matrix {
    int n, m;  //dimensions
    T** arr;

public:
    Matrix();
    Matrix(int row, int column, const T& value);
    Matrix(const Matrix<T>& copy);
    ~Matrix();
    void init(int row, int column, T** ar);

    template <typename O>
    friend std::ostream& operator<<(std::ostream& out, const Matrix<O>& t);

    Matrix<T>& operator=(const Matrix<T>& rhs);
    Matrix<T>& operator+=(const Matrix<T>& rhs);
    Matrix<T>& operator+(const Matrix<T>& rhs);
};

cpp 文件

template <typename T>
void Matrix<T>::init(int row, int column, T** a) {
    n = row;
    m = column;
    arr = new int *[n];
    for (unsigned i = 0; i < n; i++)
        arr[i] = new int[m];
    if (a) {
        for (unsigned i = 0; i < n; i++)
            for (unsigned j = 0; j < m; j++)
                arr[i][j] = a[i][j];
    }
}

template <typename T>
Matrix<T>::Matrix() {
    init(2,2, arr);
}

template <typename T>
Matrix<T>::Matrix(int row, int column, const T& value) {
    n = row;
    m = column;

    arr = new int *[n];
    for (unsigned i=0; i < n; i++)
        arr[i] = new int[m];

    for(unsigned i=0; i<n; i++)
        for(unsigned j=0; j<m; j++)
            arr[i][j] = value;
}

template <typename T>
Matrix<T>::Matrix(const Matrix<T>& mat_copy) {
    n = mat_copy.n;
    m = mat_copy.m;
    init(mat_copy.n, mat_copy.m, mat_copy.arr);
}

template <typename T>
Matrix<T>::~Matrix() {
    for(unsigned i=0; i<n; i++)
        delete[] arr[i];
    delete[] arr;
}

template <typename T>
Matrix<T>& Matrix<T>::operator=(const Matrix<T>& T1) {
    if(this == &T1) return *this;
    for(unsigned i=0; i<n; i++)
        delete [] arr[i];
    delete  [] arr;
    init(T1.n, T1.m, T1.arr);

    return *this;
 }

 template <typename T>
 std::ostream& operator<<(std::ostream& out, const Matrix<T>& t) {
     for(unsigned i=0; i<t.n; i++) {
        std::cout << std::endl;
        for(unsigned j=0; j<t.n; j++)
            out << t.arr[i][j] << " ";
    }
    std::cout << std::endl;
    return out;
}

template <typename T>
Matrix<T>& Matrix<T>::operator+(const Matrix<T>& rhs) {
    int rows = n;
    int columns = m;
    Matrix result(rows,columns,0);

    for (unsigned i = 0; i < rows; i++)
        for (unsigned j = 0; j < columns; j++)
            result.arr[i][j] = (*this).arr[i][j] + rhs.arr[i][j];

    return result;
}

任何想法如何解决这个问题?我会很感激你的帮助和建议来改进它,因为可能会有一些错误。

我使用最新的 CLion 版本和更新的 cygwin 编译器。

【问题讨论】:

  • 旁注:您应该考虑连续内存并将矩阵表示为 T 的数组,其大小为 n*m 以避免 n 分配。
  • 您确定要添加的两个矩阵大小相同吗?否则,您将进入名为 Undefined Behavior 的狂野而危险的地方。
  • @DieterLücking 我知道我可以使用一维数组来做到这一点,但我想尝试这种方式。无论如何感谢您的建议。

标签: c++ templates matrix operator-overloading


【解决方案1】:

几个cmets:

1)(可能是你问题的根源),operator&lt;&lt;方法中j迭代器的上界不对,应该是:

for(unsigned j=0; j<t.m; j++)

2) 构造函数Matrix&lt;T&gt;(int row, int column, const T&amp; value) 应该调用init 方法而不是重新实现它。

3) init 应该是私有的

4) 提供一个创建 2x2 矩阵的默认构造函数是没有意义的!默认构造函数应该创建一个空矩阵 0x0!

5) 在对矩阵求和之前,您应该检查它们是否与此类操作兼容。

6) 我希望您不是在寻找性能,因为您在内存中放置矩阵的方式不适合缓存并且不会很快!

【讨论】:

  • 1) 你说得对,我没有注意到这一点,但就我而言,它并没有改变任何东西,因为我正在相同维度的矩阵上对其进行测试。 2)我试过了,但有些东西崩溃了,所以支持这段代码。 4)这只是任务的原则,不是我的想法:)。 5)我知道,但正如我所说,我正在研究相同大小的矩阵。 6)性能不是这里的关键。这是训练练习。
【解决方案2】:

我怀疑问题是由operator+() 函数的返回值引起的。您正在返回对函数本地对象的引用。

template <typename T>
Matrix<T>& Matrix<T>::operator+(const Matrix<T>& rhs) {
    int rows = n;
    int columns = m;
    Matrix result(rows,columns,0);

    for (unsigned i = 0; i < rows; i++)
        for (unsigned j = 0; j < columns; j++)
            result.arr[i][j] = (*this).arr[i][j] + rhs.arr[i][j];

    // Returning a reference to a function local object.
    // The reference will be a dangling reference when the function returns.
    return result;
}

将返回类型更改为对象。

template <typename T>
Matrix<T> Matrix<T>::operator+(const Matrix<T>& rhs) {
    int rows = n;
    int columns = m;
    Matrix result(rows,columns,0);

    for (unsigned i = 0; i < rows; i++)
        for (unsigned j = 0; j < columns; j++)
            result.arr[i][j] = (*this).arr[i][j] + rhs.arr[i][j];

    return result;
}

【讨论】:

  • 这就是重点:)。非常感谢。我忘了返回类型:)。
  • @Saris,不客气。我很高兴能够提供帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多