【问题标题】:Overloading operator: Matrix Addition重载运算符:矩阵加法
【发布时间】:2017-03-22 12:42:24
【问题描述】:

enter image description here

我正在尝试设置我的函数并执行一些重载操作,以便我可以 +,-,==,* 两个矩阵。我在第一次操作重载时遇到了一个问题:加法。

我的程序一直有效,直到我尝试添加 2 个矩阵。

感谢您的帮助。

include<iostream>
using namespace std;

class matrixType
{
private:
    int rows,cols;
    int** matrix;
public:

    matrixType( int r, int c)
    {
        rows=r;
        cols=c;
        matrix = new int*[rows];
        for(int i = 0; i < rows; ++i)
            matrix[i] = new int[cols];
    }

    ~matrixType()
    {
        for(int i = 0; i < rows; ++i) 
        {
            delete [] matrix[i];
        }
        delete [] matrix;
    }

    matrixType operator+( matrixType m2 )
    {
        if( rows==m2.rows && cols==m2.cols)
        {
            matrixType m3(rows, cols);
            for( int i=0; i<rows; i++)
            {
                for( int j=0; j<cols; j++)
                {
                    m3.matrix[i][j]=matrix[i][j]+m2.matrix[i][j];
                }
            }
            return m3;
        }
    }

    matrixType operator-(matrixType m2)
    {
        if( rows==m2.rows && cols==m2.cols)
        {
            matrixType m3(rows, cols);
            for( int i=0; i<rows; i++)
            {
                for( int j=0; j<cols; j++)
                {
                    m3.matrix[i][j]=matrix[i][j]-m2.matrix[i][j];
                }
            }
            return m3;
        }
    }

    friend istream& operator>> (istream& stream, matrixType m)
    {
        for ( int i=0; i<m.rows;i++)
        {
            for( int j=0; j<m.cols;j++)
            {
                cout<<"Matrix"<<"["<<i<<"]"<<"["<<j<<"]"<<"=";
                stream>>m.matrix[i][j];
                cout<<endl;
            }
        }
        return stream;
    }

    friend ostream& operator<<(ostream& out, matrixType m)
    {
        for ( int i=0; i<m.rows;i++)
        {
            for( int j=0; j<m.cols;j++)
            {
                cout<<"Matrix"<<"["<<i<<"]"<<"["<<j<<"]"<<"=";
                out<<m.matrix[i][j];
                cout<<endl;
            }
        }
        return out;
    }
};

【问题讨论】:

标签: c++ matrix operator-overloading operator-keyword


【解决方案1】:

完全不同的替代方法 - 基于模板:

template <size_t Rows, size_t Columns>
class Matrix
{
    int matrix[Rows][Columns];

public:
    void operator+=(Matrix<Rows, Columns> const& other)
    {
        for(size_t i = 0; i < Rows; ++i)
        {
            for(size_t j = 0; j < Columns; ++j)
            {
                matrix[i][j] += other.matrix[i][j];
            }
        }
    }

    Matrix<Rows, Columns>
    operator+(Matrix<Rows, Columns> const& other) const
    {
        Matrix<Rows, Columns> result(*this);
        result += other;
        return result;
    }

    template<size_t C>
    Matrix<Rows, C> operator*(Matrix<Columns, C> const& other) const
    {
        // just exemplary, actual implementation missing:
        return Matrix<Rows, C>();
    }

    // rest of operators coming here
};

它可能符合您的需求,也可能不符合您的需求,但如果符合,您将免费获得三法则。此外,系统会自动阻止您添加或乘以不合适大小的矩阵。

另一方面——嗯,好处总是伴随着一些成本......——你失去了灵活性。想象一下,您想将任意矩阵放入向量中 - 您需要一个基类并且必须使用(智能?)指针,添加或乘以任意矩阵需要强制转换,...​​

不过,最大的缺点是您需要在编译时知道矩阵大小 - 如果您不知道,我们就出局了。

顺便说一下,加法/乘法 - 在您的原始实现中,如果矩阵大小不匹配,您不会返回任何内容!那么你应该返回某种哨兵,例如。 G。一个 0x0 矩阵 - 或者 可能 更好:抛出一些适当的异常。

【讨论】:

    【解决方案2】:

    这听起来像是违反rule of three 的案例。

    你需要实现一个拷贝构造函数:

    matrixType(const matrixType&)
    

    还有一个复制赋值运算符:

    matrixType operator=(const matrixType&)
    

    对于C++11,最好同时实现移动构造函数和移动赋值运算符。

    matrixType(matrixType&&)
    matrixType& operator=(matrixType&& other)
    

    【讨论】:

      猜你喜欢
      • 2016-02-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多