Skip to main content
gamedev.net
🔒 Locked

operator overloading problem

Started by cignox1 5 replies 1k views
Original Post
cignox1
cignox1
There we are, a problem with operators overloading is confusing me... Well, as stated on many sites, I overtloaded the '=' operator in the following way:

matrix &operator = (matrix &m)
                { for(int i = 0; i < 4; i++) for( int j = 0; j < 4; j++) mat[j] = m.mat[j]; return *this;}


and the '*' operator in this one:

matrix matrix::operator * (matrix &m)
{
    matrix t;
    for(int i = 0; i < 4; i++)
        for(int j = 0; j < 4; j++)
        t.mat[j] =  mat[0]*m.mat[0][j] + mat[1]*m.mat[1][j] + mat[2]*m.mat[2][j] + mat[3]*m.mat[3][j]; 
        
    return t;
}


when I compile it I get the following error from gcc (the version provided with Dev-C++ 5):

101 C:\Alessandro\progetti\quarktracer\geometry.cpp no match for 'operator=' in 'a =  matrix::operator*(matrix&)((&c))' 

what's going wrong? Thank you!
flangazor
flangazor
Unsure if it will alleviate your compile problem, but you may like to have const keyword decorating your arguments in your function signature.
matrix& operator=(const matrix& m){//blah}//...matrix m1;matrix m2(1,2,3,4,whatever);m1 = m2; // should work.
chollida1
chollida1
To overload the operator= you should always pass in the source as a const&. The error looks like your passing in a reference to a variable as opposed to passing in the variable and letting the compiler pass a reference in.

Cheers
Chris
CheersChris
SiCrane
SiCrane
The problem is actually that the operator*() overload is trying to call operator=() with an temporary on the right hand side, but the operator=() overload accepts only non-const references. Non-const references do not play happily with temporaries. The solution is still to change operator=() to accept a const reference instead of a non-const reference for the parameter type.

But keep in mind that operator=() can legally accept a non-const reference as a paramter type. For example, std::auto_ptr does this.
cignox1
cignox1
Thank you, so the const keyword must be added...now it works...just another question: where should I put const? Into every overloaded operator? Or just for '='? I would guess into every one...
MindGames
MindGames
You could always just use the rule of thumb I use; If it should never be changed by the operation, make it const.
snk_kid
snk_kid
also if any type of member function does not modifiy or need to modify the state then make it a constant member function, change your opeartor* to:

matrix operator*(const matrix& mat_ref) const;


personally thou with the general arithmetic operators you can make them free-functions & build them in terms of arithmetic assignement operators, this save you alot of coding time instead of repeating the virutally the same code e.g.

class foo;foo operator+(const foo&, const foo&);class foo {   float x, y;public:  //....  foo& operator+=(const foo& f) {     x += f.x; y += f.y;     return *this;  }};inline foo operator+(const foo& f1, const foo& f2) {   return (foo(f1) += f2);}

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.