C ++ type error with Object to Object reference
I have the following function (working in Visual Studio):
bool Plane::contains(Vector& point){
return normalVector.dotProduct(point - position) < -doubleResolution;
}
When I compile it with g ++ version 4.1.2 I get the following error:
Plane.cpp: In member function รขvirtual bool Plane::contains(Vector&)รข:
Plane.cpp:36: error: no matching function for call to รขVector::dotProduct(Vector)รข
Vector.h:19: note: candidates are: double Vector::dotProduct(Vector&)
So, as you can see, the compiler thinks (point-to-point) is a vector, but it expects a Vector &.
What's the best way to fix this?
I have verified that this works:
Vector temp = point-position;
return normalVector.dotProduct(temp) < -doubleResolution;
But I was hoping for something a little cleaner.
I heard a suggestion that adding a copy constructor might help. So I added a copy constructor to Vector (see below), but that didn't help.
vector.h:
Vector(const Vector& other);
Vector.cpp:
Vector::Vector(const Vector& other)
:x(other.x), y(other.y), z(other.z), homogenous(other.homogenous) {
}
a source to share
Your problem is that the result point - position
is a temporary object that cannot be bound to a non-const reference.
If the function does not change the argument taken by reference, then it must accept a reference to a constant. Ergo, your dot product function should be declared as:
double Vector::dotProduct(const Vector&);
a source to share