繁体   English   中英

重载矩阵 class 的加法和乘法运算符

[英]overloading the addition and multiplication operators for the matrix class

我为矩阵 class 实现了加法和乘法运算符的重载。 我不明白错误在哪里,或者更确切地说,我猜是在声明运算符时有错误,但我不知道如何具体修复它。 有人可以推荐一本关于 object 面向编程的好书。 我觉得我显然缺乏信息

#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

我相信 std::vector 更好

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

你也可以代理行和列来重载[]

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

也欢迎生成 function

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

https://godbolt.org/z/dK6Tqc

暂无
暂无

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

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