简体   繁体   中英

overloading the addition and multiplication operators for the matrix class

I implement the overloading of the addition and multiplication operators for the matrix class. I can't understand where the error is, or rather, I guess that there is an error in the declaration of operators, but I have no idea how to fix it specifically. Can someone suggest a good book on object oriented programming. I feel that I am clearly lacking information

#include <iostream>
using namespace std;


template <typename T>
class MATRIX
{
private:
    T** M; 
    int m; 
    int n; 

public:
    
    MATRIX()
    {
        n = m = 0;
        M = nullptr; 
    }

    
    MATRIX(int _m, int _n)
    {
        m = _m;
        n = _n;

        
        M = (T**) new T * [m]; 

        
        for (int i = 0; i < m; i++)
            M[i] = (T*)new T[n];

       
        for (int i = 0; i < m; i++)
            for (int j = 0; j < n; j++)
                M[i][j] = 0;
    }

    
    MATRIX(const MATRIX& _M)
    {
        
        m = _M.m;
        n = _M.n;

        
        M = (T**) new T * [m];
        for (int i = 0; i < m; i++)
            M[i] = (T*) new T[n];

        
        for (int i = 0; i < m; i++)
            for (int j = 0; j < n; j++)
                M[i][j] = _M.M[i][j];
    }

   
    T GetMij(int i, int j)
    {
        if ((m > 0) && (n > 0))
            return M[i][j];
        else
            return 0;
    }

    void SetMij(int i, int j, T value)
    {
        if ((i < 0) || (i >= m))
            return;
        if ((j < 0) || (j >= n))
            return;
        M[i][j] = value;
    }

   
    void Print(const char* ObjName)
    {
        cout << "Object: " << ObjName << endl;
        for (int i = 0; i < m; i++)
        {
            for (int j = 0; j < n; j++)
                cout << M[i][j] << "\t";
            cout << endl;
        }
        cout << "---------------------" << endl << endl;
    }

    
    MATRIX operator=(const MATRIX& _M)
    {
        if (n > 0)
        {
           
            for (int i = 0; i < m; i++)
                delete[] M[i];
        }

        if (m > 0)
        {
            delete[] M;
        }

        
        m = _M.m;
        n = _M.n;

        
        M = (T**) new T * [m]; 
        for (int i = 0; i < m; i++)
            M[i] = (T*) new T[n];

        
        for (int i = 0; i < m; i++)
            for (int j = 0; j < n; j++)
                M[i][j] = _M.M[i][j];
        return *this;
    }
    MATRIX operator+(const MATRIX& _M) {
        MATRIX M6(_M.n, _M.m);
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < m; j++)
                M6.M[i][j] = _M.M[i][j]+this-> M[i][j];


        }
        return M6;

    }

    MATRIX operator*(const MATRIX& _M) {
        MATRIX M5(_M.n, _M.m);
        for (int i = 0; i < _M.n; i++)
        {
            for (int j = 0; j < _M.m; j++)
            {
                for (int k = 0; k < m; k++)
                {

                    M5.M[i][j] += M[i][k]*_M.M[k][j];
                }
            }
        }
        return M5;


    }
    bool operator ==(const MATRIX& _M) {
        return this->n == _M.n && this->m == _M.m;
    }
    bool operator !=(const MATRIX& _M) {
        return !(this->n == _M.n && this->m == _M.m);
    }
    
    ~MATRIX()
    {
        if (n > 0)
        {
            
            for (int i = 0; i < m; i++)
                delete[] M[i];
        }

        if (m > 0)
            delete[] M;
    }
};

void main()
{
    
    MATRIX<int> M(2, 3);
    M.Print("M");

    
    int i, j;
    for (i = 0; i < 2; i++)
        for (j = 0; j < 3; j++)
            M.SetMij(i, j, i + j);

    M.Print("M");

    MATRIX<int> M2 = M; 
    M2.Print("M2");

    MATRIX<int> M3; 
    M3 = M;
    M3.Print("M3");

    MATRIX<int> M4;
    M4 = M3 = M2 = M; 
    M4.Print("M4");
    ////////////////////////
    bool result = M == M3;//  ==
    if (result == 1) {
        cout << "true";
    }
    else if (result == 0) {
        cout << "false";
    }
    cout << endl << "---------------------" << endl << endl;
    //////////////////////////
    bool result1 = M != M3;//  !=
    if (result1 == 1) {
        cout << "true";
    }
    else if (result1 == 0) {
        cout << "false";
    }
    cout << endl << "---------------------" << endl << endl;
    MATRIX<int> M1;
    for (i = 0; i < 2; i++)
        for (j = 0; j < 3; j++)
            M1.SetMij(i, j, i + j);
    M1.Print("M1");
    
    M.Print("M");
    MATRIX<int> M6 = M + M1;
    M6.Print("M");
     MATRIX<int> M5 = M * M1;
     M5.Print("M");
}```


thanks in advance


[enter image description here][1]


  [1]: https://i.stack.imgur.com/TjEOp.png

I belive that std::vector is better

MATRIX() {}
MATRIX(int r, int c) : M(r * c, T()) , rows(r), cols(c) { }
// Does not need copy constructor
// Does not need operator =
// Does not need destructor

also you can proxy row and cols to overload []

MATRIX<int> x(3,3);
x[2][2] = 10; 

also a generating function is welcome

template<typename GenT> void generate( GenT gen ) {...}

https://godbolt.org/z/dK6Tqc

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