Overloading Operators

(I think that is what it is called)...

I'm currently building a matrix class and I'm trying overload the * and + operators. See some code below:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34

class Matrix{

    public:

        float **pMatrix;
        int nRows, nCols;

        Matrix(int rows, int columns);
        Matrix();

        void setValue(int row, int col, float value);
        void outputMatrix();

        Matrix& operator * (Matrix& m);
        Matrix& operator * (float& m);


};

...

void FlightSimulator::calcNewX(Matrix& X, float U[], float dt)
{


    Matrix Xdot1(13,1);
    calcStateRates(X, U, Xdot1);

    Matrix An = Xdot1*dt;

    Matrix Xdot2(13,1);
    calcStateRates((X+0.5*An, U, Xdot2); //<---- ERROR


Now the error I'm getting is: "no match for 'operator*' in '5.0e-1 * An'"

I can't see why the initialisation of An wouldn't cause an error but this does?

Any help would be awesome,

Nick :P
Have you actually overloaded the operators? I can just see the [overloaded operator declarations?].
Yeah, here is one:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Matrix& Matrix::operator * (float& m)
{

    int rows = (*this).nRows;
    int cols = (*this).nCols;

    Matrix result(rows,cols);

    for(int i=0; i<rows; i++)
    {

        for(int j=0; j<cols; j++)
        {
            tVal = m*pMatrix[i][j];
            result.setValue(i,j,tVal);

        }

    }

    return result;


}


I have overloaded operators + and / also, but I figured if I can solve the problem with this one I should be able to figure out the others.

Thanks!
Overload Matrix operator*(float, const Matrix&); too. This way you can use operator* if the float is on left hand side of the operator.

Also BIG problem : in your Matrix& Matrix::operator * (float& m) definition you returning a reference (Matrix&) of a local object (result). You should return it by value not reference.
Other problem : the parameter is : float& m, change it to either float mor const float& m. Same goes for Matrix& m.

Since
Matrix operator * (const Matrix& m); and
Matrix operator * (float m); (corrected declarations)
doesn't change any members in this you should declare them as const functions : example :
Matrix operator * (const Matrix& m) const;

BTW Where do you declare tVal?
Hey R0mai, thanks for that awesome reply!

Went through and fixed all that stuff you pointed out lol I'm not sure why I was returning the result by reference, I'm still working my way through this pointer stuff lol

tVal was the problem, the compiler just wasn't pointing me there lol

Everything works now, but I tried to overload:
Matrix operator*(float, const Matrix&);
and failed. Do I define this in my Matrix class of somewhere else? Here was my attempt and the error that followed lol:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28


class Matrix{

    public:

        float **pMatrix;
        int nRows, nCols;

        Matrix(int rows, int columns);
        Matrix();

        void setValue(int row, int col, float value);
        void outputMatrix() const;

        Matrix operator * (Matrix m) const;

        Matrix operator * (float m) const;
        Matrix operator * (float m, Matrix mat) const;

        Matrix operator + (Matrix m) const;

        Matrix operator / (float m) const;
        Matrix operator / (float, Matrix) const;


};


Error: Matrix Matrix::operator*(float, Matrix)' must take either zero or one argument

I did go and read the tutorial on overloading before I posted here. I think this is what I should be looking at:


a*b A::operator* (B) operator*(A,B)


Which would mean in this case i.e. float*Matrix, I'd need to go:

1
2
3

float::operator* (Matrix)


Am I even close to figuring this out? lol

Anyway, your help is awesome!

Nick

*bump
Define Matrix operator * (float m, Matrix mat) const; (and alike) outside the Matrix class. If you need to access private/protected members of the Matrix class in these functions then declare them as friend functions :

1
2
3
4
5
6
7
8
9
10
11
12
13
class Matrix{
public:
//...
      friend Matrix operator * (float m, Matrix mat) const;

//...
};

//a simple implementation
Matrix operator * (float m, Matrix mat) const {
     return mat*m;
}


http://www.cplusplus.com/doc/tutorial/inheritance/
Last edited on
Excellent! Thanks so much :)

Nick
Topic archived. No new replies allowed.