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) {
}

      

+2


a source to share


4 answers


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&);

      

+2


a source


A Vector

temporary variable cannot be properly converted to Vector&

- I think MSVC ++ is too weak here. Why do contains

they dotProduct

accept Vector&

where they really don't need to change arg ?! They must take const Vector&

! I think gcc is guiding you correctly here.



+2


a source


point - position

seems to be creating a temporary object of the type Vector

and you are trying to pass temporary functions that require reference. This is not permitted. Try to declare it asdotProduct(const Vector&);

+1


a source


The problem is that your dotProduct function must take its parameter via a const reference.

+1


a source







All Articles