简体   繁体   中英

C++, overload * for matrix multiplication

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')

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 .

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.

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