C++, overload * for matrix multiplication

南笙酒味 提交于 2021-02-11 08:50:24

问题


I'm having a great deal of trouble trying to overload the multiplication operator * for matrix multiplication. I've defined a matrix class

#ifndef MMATRIX_H
#define MMATRIX_H
#include <vector>
#include <cmath>

// Class that represents a mathematical matrix
class MMatrix
{
public:
// constructors
MMatrix() : nRows(0), nCols(0) {}
MMatrix(int n, int m, double x = 0) : nRows(n), nCols(m), A(n * m, x)
{}

// set all matrix entries equal to a double
MMatrix &operator=(double x)
{
    for (int i = 0; i < nRows * nCols; i++) 
        A[i] = x;
return *this;
}

// access element, indexed by (row, column) [rvalue]
double operator()(int i, int j) const
{
    return A[j + i * nCols];
}

// access element, indexed by (row, column) [lvalue]
double &operator()(int i, int j)
{
    return A[j + i * nCols];
}


// size of matrix
int Rows() const { return nRows; }
int Cols() const { return nCols; }

// operator overload for matrix * vector. Definition (prototype) of member class
MVector operator*(const MMatrix& A);

private:
unsigned int nRows, nCols;
std::vector<double> A;
};
#endif

And here is my attempted operator overload

inline MMatrix operator*(const MMatrix& A, const MMatrix& B)
{
MMatrix m(A), c(m.Rows(),m.Cols(),0.0);
for (int i=0; i<m.Rows(); i++)
{
    for (int j=0; j<m.Cols(); j++)
    {
        for (int k=0; k<m.Cols(); k++)
        {
            c(i,j)+=m(i,k)*B(k,j);
        }
    }
}
return c;

}

I'm sure that there is nothing wrong with the actual multiplication of elements.

The error that I get is from my main .cpp file where I have tried to multiply two matrices together C=A*B; and I get this error,

error: no match for 'operator=' (operand types are 'MMatrix' and 'MVector')


回答1:


There are 2 ways to overload operator*:

MMatrix MMatrix::operator*(MMatrix); //or const& or whatever you like
MMatrix operator*(MMatrix, MMatrix);

These are both valid, but different with slightly different semantics.

For your definition to match your declaration change the definition to:

MMatrix MMatrix::operator*(const MMatrix & A)
{
    //The two matrices to multiple are (*this) and A
    MMatrix c(Rows(),A.Cols(),0.0);
    for (int i=0; i < Rows(); i++)
    {
        for (int j=0; j < A.Cols(); j++)
        {
            for (int k=0; k < Cols(); k++)
            {
                c(i,j) += (*this)(i,k)*A(k,j);
            }
        }
    }
    return c;
}

As for the error you're seeing, it seems in your class you declared the operator to take a matrix and return a vector. You probably meant to return a matrix instead. The error is telling you that you can't assign a MVector to a MMatrix.




回答2:


I believe, you need to define copy constructor and copy assignment:

MMatrix(const MMatrix& other);
MMatrix& operator=(const MMatrix& other);

Move constructor and assignment wouldn't heart either:

MMatrix(MMatrix&& other);
MMatrix& operator=(MMatrix&& other);

Same goes to your MVector.



来源:https://stackoverflow.com/questions/40635337/c-overload-for-matrix-multiplication

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!