简体   繁体   中英

Overloading operator: Matrix Addition

enter image description here

I'm trying to set up my functions and perform some overloading operations so that I can +,-,==,* two matrices. I have encountered a problem at the first operation overload: addition.

My program works until i try to add 2 matrices.

Thanks for help.

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

Totally different approach as alternative - based on templates:

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

It might or might not fit your needs, but if it does, you get the rule of three for free. Additionally you are prevented automatically from adding or multiplying matrices of non-fitting sizes.

On the other hand -- well, benefits always come with some cost, too... -- you lose flexibility. Imagine you want to place arbitrary matrices into a vector - you'd need a base class then and would have to use (smart?) pointers, adding or multiplying arbitrary matrices requires casts, ...

Biggest drawback, though, is that you need to know your matrix sizes at compile time - if you don't, we are out.

By the way, adding/multiplying - in your original implementation, you do not return anything if matrix sizes do not match! You should return some kind of sentinel then, eg a 0x0 matrix - or possibly even better: throw some appropriate exception.

This sound like a case of rule of three violation.

You need to implement a copy constructor:

matrixType(const matrixType&)

And a copy assignment operator:

matrixType operator=(const matrixType&)

And for C++11 it might be a good idea to also implement the move constructor and move assignment operator.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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