C++ 重载运算符:矩阵加法

C++ 重载运算符:矩阵加法,c++,matrix,operator-overloading,operator-keyword,C++,Matrix,Operator Overloading,Operator Keyword,我正在尝试设置我的函数并执行一些重载操作,这样我就可以使用+,-,==,*两个矩阵。我在第一次操作重载时遇到了一个问题:加法 我的程序一直工作,直到我尝试添加2个矩阵 谢谢你的帮助 include<iostream> using namespace std; class matrixType { private: int rows,cols; int** matrix; public: matrixType( int r, int c) {

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

我的程序一直工作,直到我尝试添加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;
    }
};
包括
使用名称空间std;
类矩阵类型
{
私人:
int行,cols;
int**矩阵;
公众:
矩阵类型(int r,int c)
{
行=r;
cols=c;
矩阵=新整数*[行];
对于(int i=0;i
您需要实现一个复制构造函数:

matrixType(const matrixType&)
和一个复制分配操作员:

matrixType operator=(const matrixType&)
对于
C++11
,最好也实现move构造函数和move赋值操作符

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

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

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
};
模板
类矩阵
{
int矩阵[行][列];
公众:
void运算符+=(矩阵常数和其他)
{
对于(大小i=0;i
它可能适合您的需要,也可能不适合您的需要,但如果符合,您将免费获得三个规则。此外,您将无法自动添加或乘以不适合大小的矩阵

另一方面——好吧,好处总是伴随着一些成本…——你失去了灵活性。假设你想把任意矩阵放到一个向量中——你需要一个基类,然后必须使用(智能?)指针,添加或乘以任意矩阵需要强制转换

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


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

有什么问题吗?错误是什么?好的参考指南:查找“三法则”首先。