Question

I just implemented my own matrix class that has some functions, i wrote = and * operator overload functions for copying and multiplying matrices, however when i run a test program i got the following error:

Invalid Operands to binary Expression (Matrix4X4 and Matrix4X4)

my main Code:

#include <iostream>
#include "Matrix4X4.h"

using namespace std;

int main(int argc, const char * argv[])
{

Matrix4X4 mat(4);
Matrix4X4 sat;
Matrix4X4 result;

sat.setTranslation(5, 2, 6);


//////////// just printing code nothing worth looking for ///////////////////////////
float * ptr = mat.getMat();

for (int i = 0 ; i < 16; i += 4)
    cout << ptr[i] << "   " << ptr[i+1] << "   " << ptr[i+2] << "   " << ptr[i+3] << endl;

cout << "\n\n\n" << endl;

ptr = sat.getMat();

for (int i = 0 ; i < 16; i += 4)
    cout << ptr[i] << "   " << ptr[i+1] << "   " << ptr[i+2] << "   " << ptr[i+3]  << endl;
////////////////////////////// end of printing code //////////////////////////////////////


result = mat * sat;       //  <<<< line that generates the error.



return 0;
}

Matrix4X4.cpp file:

#include "Matrix4X4.h"


Matrix4X4::Matrix4X4()
{
setIdentity();
}

Matrix4X4::Matrix4X4(float value)
{
for(int i = 0 ; i < 4; i++)
    for ( int j = 0; j < 4; j++)
        Matrix[i][j] = value;

}


Matrix4X4::~Matrix4X4()
{

}


void Matrix4X4::setIdentity()
{
Matrix[0][0] =1;   Matrix[0][1] = 0;  Matrix[0][2] = 0;      Matrix[0][3] = 0;
Matrix[1][0] =0;   Matrix[1][1] = 1;  Matrix[1][2] = 0;      Matrix[1][3] = 0;
Matrix[2][0] =0;   Matrix[2][1] = 0;  Matrix[2][2] = 1;      Matrix[2][3] = 0;
Matrix[3][0] =0;   Matrix[3][1] = 0;  Matrix[3][2] = 0;      Matrix[3][3] = 1;

}


void Matrix4X4::setTranslation(float x,float y,float z)
{
Matrix[0][0] =1;   Matrix[0][1] = 0;  Matrix[0][2] = 0;      Matrix[0][3] = x;
Matrix[1][0] =0;   Matrix[1][1] = 1;  Matrix[1][2] = 0;      Matrix[1][3] = y;
Matrix[2][0] =0;   Matrix[2][1] = 0;  Matrix[2][2] = 1;      Matrix[2][3] = z;
Matrix[3][0] =0;   Matrix[3][1] = 0;  Matrix[3][2] = 0;      Matrix[3][3] = 1;

}




float * Matrix4X4::getMat()
{
return (float *) Matrix;
}


float Matrix4X4::getMember(int x, int y)
{
return Matrix[x][y];
}


void Matrix4X4::setMat(int row,int col,float value)
{
Matrix[row][col] = value;
}




Matrix4X4 operator * (Matrix4X4 & lhs,Matrix4X4 & rhs)
{

Matrix4X4 result;

    for(int i = 0 ; i < 4; i++)
        for ( int j = 0; j < 4; j++)
            result.setMat(i, j,  lhs.getMember(i,0) * rhs.getMember(0, j) +
                            lhs.getMember(i,1) * rhs.getMember(1, j) +
                            lhs.getMember(i,2) * rhs.getMember(2, j) +
                            lhs.getMember(i,3) * rhs.getMember(3, j));


        return result;
}

Matrix4X4.h file:

#ifndef ___D_GameDev_Kit__Matrix4X4__
#define ___D_GameDev_Kit__Matrix4X4__

#include <iostream>







class Matrix4X4
{
private:
float  Matrix[4][4];

public:

Matrix4X4();
Matrix4X4(float);
~Matrix4X4();


void setIdentity();
void setTranslation(float,float,float);
void setRotation();
void setScale(float,float,float);
void setPerspective();
void setOrthogonal();

float * getMat();
float getMember(int,int);
void setMat(int,int,float);



Matrix4X4 & operator = (Matrix4X4 & rhs)
{
    if(this!=&rhs)
        for(int i = 0 ; i < 4; i++)
            for (int j = 0; j < 4; j++)
                Matrix[j][i] = rhs.getMember(j, i);
                return *this;
}
};


#endif /* defined(___D_GameDev_Kit__Matrix4X4__) */
Was it helpful?

Solution

It looks like you have implemented the operator correctly, but you have forgotten to add a forward declaration for it to the header.

Add this line to your Matrix4X4.h file to fix this problem:

Matrix4X4 operator * (Matrix4X4 & lhs, Matrix4X4 & rhs);

It needs to be added outside the class declaration, because it is a free-standing binary operator.

Note that you could improve your code by marking float getMember(int,int); constant, and declaring lhs and rhs parameters const references:

float getMember(int,int) const;
Matrix4X4 & operator = (const Matrix4X4 & rhs) // Make your assignment operator take a const

...
Matrix4X4 operator * (const Matrix4X4 &lhs, const Matrix4X4& rhs);

OTHER TIPS

The module that contains function main knows nothing about operator * because the header that included in the module does not have the declaration of the operator. You have to include the declaration of the operator in the header.

Also the operator should be declared as

Matrix4X4 operator * ( const Matrix4X4 & lhs, const Matrix4X4 & rhs);

The copy assignment operator also should be declared the same way that is the parameter should have qualifier const

Matrix4X4 & operator = ( const Matrix4X4 & rhs );

The * operator could be a member too :

Matrix4X4 operator *(const Matrix4X4& rhs);

and lhs would be (*this).

In both case, it should appear in your header.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top