1

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

James M
16.7k3 gold badges50 silver badges57 bronze badges
asked Mar 29, 2012 at 0:13

2 Answers 2

5

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.

answered Mar 29, 2012 at 0:15
Sign up to request clarification or add additional context in comments.

Comments

2

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);
answered Mar 29, 2012 at 0:17

5 Comments

Cheers for the help, but changing them all to const gives me 'discards qualifiers' errors. Sorry, I only started c++ yesterday :/
Maybe you could provide us with a complete example that demonstrates the problem you're having. We can only guess at what the rest of your code looks like, and that's not productive for anybody.
'discards qualifiers' means you're calling something in the function that doesn't take a const, but probably should. Show us more code.
@user1135488: The * 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
More on operator overloading here

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.