views:

100

answers:

4

I have the following function (which worked in Visual Studio):

bool Plane::contains(Vector& point){
    return normalVector.dotProduct(point - position) < -doubleResolution;
}

When I compile it using 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-position) is a Vector but it's expecting Vector&.

What's the best way to fix this?

I verified that this works:

Vector temp = point-position;
return normalVector.dotProduct(temp) < -doubleResolution;

But I was hoping for something a little bit cleaner.

I heard a suggestion that adding a copy constructor might help. So I added a copy constructor to Vector (see below), but it 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: 

Your problem is that the result of point - position is a temporary object, which cannot be bound to a non-const reference.

If a function does not modify an argument taken by reference, then it should take a const reference. Ergo, your dot product function should be declared as:

double Vector::dotProduct(const Vector&);
James McNellis
Thanks, that fixed the problem. Thanks to everyone else who suggested a similar answer too.
muddybruin
+1  A: 

point - position seems to creates temporary object of type Vector and you are trying to pass temporary to a function that requires reference. It is not allowed. Try declare it as dotProduct(const Vector&);

Kirill V. Lyadvinsky
+2  A: 

A Vector temporary variable can't properly be converted to a Vector& -- I guess MSVC++ is being too lax here. Why do contains and dotProduct take a Vector& where they never really need to modify the arg?! They should take a const Vector&! I think gcc is guiding you correctly here.

Alex Martelli
I believe VC++ will issue a warning about it, but it will still let it compile, so yes, it's a bit more lax.
Dean Harding
+1  A: 

The problem is that your dotProduct function should take its parameter by const reference.

Michael Aaron Safyan
The other vector is the implicit parameter (this).
Matthew Flaschen
@Matthew, right... wasn't thinking. For some reason I didn't see the "normalVector." before the invocation.
Michael Aaron Safyan