I am trying to overload the * operator in order to multiply two objects (dot product).
These objects can be Point3D, Vector3D or Normal. They all basically have the same structure (an x, y, and z value), I've only split them up for semantics.
The line that is giving me trouble is this:
float t = (point - r.origin)*normal / (r.direction * normal);
Here, point and r.origin are a Point3D, normal is a Normal and r.direction is a Vector3D. In the header files for these classes I have the following overload functions:
In Vector3D.h:
Vector3D operator *(Normal& n);
In Point3D.h:
Point3D operator -(Point3D& p);
Point3D operator *(Normal& n);
The error I get is:
No match for 'operator*' in 'r->Ray::direction * ((const Plane*)this)->Plane::normal'
Assuming I have correctly filled out the functions in the .cpp file, is there anything stupid I've done here? Also, ignore the fact that there is no division operator yet...It's in the pipeline!
Cheers
2 Answers 2
You may have defined the unary versions instead of the binary versions. (Did you define them inside the class { ... } ? )
Or you may have not defined them as const. Try this:
Vector3D operator *(const Normal& n) const;
Point3D operator -(const Point3D& p) const;
Point3D operator *(const Normal& n) const;
For binary functions over hetrogeneous parameters like this I find it a lot easier to define them all together at global scope in a seperate file/place and friend them from the classes.
Comments
Your this appears like it might be const (just guessing from the error message, since you haven't included that code in your question). Try:
Vector3D operator *(const Normal& n);
Point3D operator -(const Point3D& p);
Point3D operator *(const Normal& n);
5 Comments
const, but probably should. Show us more code.* operator that multiplies a Vector3D times a Normal should not modify either of the two arguments. The discards qualifiers compiler error is telling you that you have tried to modify the Normal argument inside the operator which will be surprising to callers: Why on earth did my normal change and where? You should actually make it even more const: Vector3D operator*( const Normal& n ) const (both arguments are const!) or as a free function Vector3D operator*( const Vector3D& v, const Normal& n ), and ensure that your code is const-correct