1

Im trying to fix the example code in this question: How to convert a 3D point into 2D perspective projection?

I've ironed out most of the mistakes, but one leaves me puzzled.

There's a class Matrix with an [] operator

float& operator[]( size_t index )
{
    if (index >= 16) {
        std::out_of_range e( "" );
        throw e;
    }
    return data[index];
}

and I get an non-matching-operator error on this code:

inline Vector operator*( const Vector& v, const Matrix& m )
{
    Vector dst;
    dst.x = v.x*m[0] + v.y*m[4] + v.z*m[8] + v.w*m[12];
    dst.y = v.x*m[1] + v.y*m[5] + v.z*m[9] + v.w*m[13];
    dst.z = v.x*m[2] + v.y*m[6] + v.z*m[10] + v.w*m[14];
    dst.w = v.x*m[3] + v.y*m[7] + v.z*m[11] + v.w*m[15];
    return dst;
}

I can't figure out what is wrong with the operator definition.

Community
  • 1
  • 1
antipattern
  • 743
  • 1
  • 9
  • 20

1 Answers1

1

Precisely 30 seconds after formulating the problem, it encountered me that the operator* is receiving a const Matrix object.

Problem is here that operator[] is not a const function.

Leaving this here, because there were only two different Questions on google-

antipattern
  • 743
  • 1
  • 9
  • 20