c++ - 矩阵模板加法运算符重载

标签 c++ templates matrix operator-overloading

我的任务是编写一个具有某些特定功能的模板 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 编译器。

最佳答案

我怀疑问题出在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;
}

关于c++ - 矩阵模板加法运算符重载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33924957/

相关文章:

c++ - LLVM IR 中 '&&' 的指令是什么?

boost ptr_vector 的 C++ 成员模板

c++ - 函数 C++ 中具有大小返回类型的模板?

java - 如何将 List<int[]> 转换为二维数组?

python - 基于 Numpy 中的其他数组对数组中的数据求和

python - 从 Python 到 MATLAB 的矩阵

c++ - 如何在 cppunit 中断言语句抛出 Excp1 或 Excp2 类型的异常?

c++ - Luarocks luasql-mysql 编译错误

c++ - 如何实现 cv::Mat 对象的循环缓冲区(OpenCV)?

c++ - C++ 中的函数模板问题