简体   繁体   English

矩阵模板添加运算符重载

[英]Matrix template addition operator overloading

I have a task to write a template Matrix class with some specific functions. 我有一个任务是编写一个带有一些特定功能的模板Matrix类。 operator= , copy constructor and parameter constructor work fine I think. 我认为operator=copy constructorparameter constructor工作正常。 There is a problem with operator+ . operator+存在问题。 For example when I add 2 matrixes filled only with number 5 I got strange numbers in whole row 1 and whole column m without element in last row. 例如,当我添加2个仅填充数字5的矩阵时,我在整row 1得到奇怪的数字,而整column m在最后一行没有元素。 Something like that (o - okay, x - wrong result). 这样的事情(o - 好的,x - 错误的结果)。

xxxx
ooox
ooox
oooo

header file 头文件

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 file 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;
}

Any ideas how to fix that? 任何想法如何解决? I would be grateful for your help and advices with improving it because probably there will be some mistakes. 我很感激你的帮助和建议改进它,因为可能会有一些错误。

I use the newest CLion release with updated cygwin compiler. 我使用最新的CLion版本和更新的cygwin编译器。

I suspect the problem is caused by the return value of the operator+() function. 我怀疑问题是由operator+()函数的返回值引起的。 You are returning a reference to an object that is local to the function. 您将返回对函数本地对象的引用。

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;
}

Change the return type to be an object. 将返回类型更改为对象。

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;
}

Several comments: 几条评论:

1) (probably the source of your problem), the upper-bound of the j iterator in the operator<< method is wrong, it should be: 1)(可能是你的问题的根源), operator<<方法中的j迭代器的上限是错误的,它应该是:

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

2) The constructor Matrix<T>(int row, int column, const T& value) should be calling the init method and not reimplement it. 2)构造函数Matrix<T>(int row, int column, const T& value)应该调用init方法而不是重新实现它。

3) init should be private 3) init应该是私有的

4) It makes no sense to provide a default constructor which creates a 2x2 matrix! 4)提供一个创建2x2矩阵的默认构造函数是没有意义的! The default constructor should create an empty matrix 0x0! 默认构造函数应该创建一个空矩阵0x0!

5) Before summing the matrices you should check whether they are compatible for such operation. 5)在对矩阵求和之前,应检查它们是否与此类操作兼容。

6) I hope you are not looking for performance because the way you laid down the matrix in memory is not cache friendly and not going to be fast! 6)我希望你不是在寻找性能,因为你在内存中放置矩阵的方式不是缓存友好而且不会很快!

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

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