简体   繁体   中英

3D Vectors and constness

I'm implementing a function which takes in 3 vectors thus:

mat4 lookAt(const vec3 &eye, const vec3 &centre, const vec3 &up);

The problem arises when I want to do some vector maths:

auto s = centre - eye; // won't compile
auto a = s.normalise();

When s attempts to be compiled, the compiler throws this error:

error: passing ‘const vec3’ as ‘this’ argument discards qualifiers [-fpermissive]

And this is how I implement subtractor overloading in my vec3 class:

vec3 vec3::operator -(const vec3& v)
{
    *this -= v;
    return *this;
}

Which ultimately comes from:

vec3& vec3::operator -=(const vec3& v)
{
    this->x -= v.x;
    this->y -= v.y;
    this->z -= v.z;

    return *this;
}

I'm not sure what's causing the compiler error to prop up, so:

  1. I changed auto to explicit types, prefixed by const; eg const vec3 a = s.normalise(); . This didn't work.

  2. It could have something to do with the subtractor overload wherein it's returning a reference.

I have checked my copy constructor, and my assignment overload - they both function fine; both are used in other pieces of code and working as intended.

Just so you are also aware, the lookAt function is declared and implemented within the implementation file. It's neither a member of a class nor used in a header file. Might that confuse the compiler?

I need a third eye for this as I can't figure out the exact context of the error in question!

The reason for the compilation error is that operator- is not const , but you are applying it via const reference. Only const members can be applied to objects via const references. The signature needs to be

vec3 vec3::operator -(const vec3& v) const
                                     ^^^^^

Then you need to modify the implementation such that it doesn't modify this , which it should never do anyway ( a - b should not modify a or b ). For example,

vec3 vec3::operator -(const vec3& v) const
{
    vec3 res = *this;
    res -= v;
    return res;
}

Note that it is customary to implement operator- as a non-member, to make the treatment of RHS and LHS operands symmetric:

vec3 operator-(const vec3& lhs, const vec3& rhs)
{
  vec3 res = lhs;
  res -= rhs;
  return rhs;
}

You have a problem in the implementation of the - operator . In the implementation you are changing the value of this which not correct. when you subtract two variable, you do not change any of them. Thus leads to error because you subtracting auto s = centre - eye; and centre is const while the - operator is trying trying to edit it.

This is the right way to overload - operator :

vec3 vec3::operator -(const vec3& v) const
{
    vec3 temp;
    temp.x=this->x-v.x;
    temp.y=this->y-v.y;
    temp.z=this->z-v.z;
    return temp;
}

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