Reputation: 11
This is a component of a larger OpenGL project. It defines a three-dimensional vector structure for use in doing some of the physics and graphics in said program. Apparently, my professor can get this to compile problem-free in Visual Studio, but when I try to compile it in any g++-based system it generates errors. These errors are as follows:
/Point3f.h:67: error: no match for 'operator*=' in '((const Vector3f&)((const Vector3f*)v1)) *= scale'
/Point3f.h:77: error: no match for 'operator+=' in '((const Vector3f&)((const Vector3f*)v1)) += v2'
/Point3f.h:82: error: no match for 'operator-=' in '((const Vector3f&)((const Vector3f*)v1)) -= v2'
I understand that lines 40-63 are overloading the *=, +=, and -= operators. What I'm confused about is why Visual Studio lets this compile and G++ wont. Any ideas?
Upvotes: 1
Views: 623
Reputation: 35141
Because the first operands of the overloaded functions aren't const. But they shouldn't be (in fact, can't be).
But you don't have the copy ctor that the non-assignment operators use. Try adding the copy ctor.
Vector3f(const Vector3f& rhs):X(rhs.X), Y(rhs.Y), Z(rhs.Z){}
On edit: or maybe I'm completely off base. :)
And why are the op= operators written as friends? They don't need to be, as they aren't symmetric.
Upvotes: 0
Reputation: 2196
57: friend Vector3f& operator *= (Vector3f& v1, float scale)
67: return Vector3f(v1) *= scale;
You can't bind a temporary (in line 67) to a non-const ref (in line 57).
For each operator, write them as:
struct Vector3f {
// ...
Vector3f& operator*=(float scale) {
// ...
return *this;
}
friend Vector3f operator*(Vector3f v, float scale) {
return v *= scale;
}
friend Vector3f operator*(float scale, Vector3f v) {
return v *= scale;
}
Upvotes: 2