Domanda

I am developing a ray tracing project. So, I have lots of vector operations. For product operation, I used operator overloading, and faced with a problem. You can see the details below:

These functions are in a header file named RayMath.h

//GENERAL INLINES
inline Vector operator*( float c, const Vector& v){ return v * c;   } //Func1
inline Vector operator*( const Vector& v1, Vector& v2 ) { return Vector( v1.x * v2.x, v1.y * v2.y, v1.z * v2.z ); } //Func 2

If I add Func 2, it gives C2666, more than one operator for the operations that use Func1. If I do not add Func 2, I get a no operator matches error. Here is the usage example:

These lines are in a .cpp file named Renderer.cpp

Vector R = ray.direction - 2.f * Dot( ray.direction, N ) * N; //Func1 related
color += trace(  Ray( ( intersectionP + R * EPSILON ), R ) ) * sphere->surfaceColor * sphere->reflection; // Func 2 related

I would like to thank you for your help!

È stato utile?

Soluzione

You have already discovered that the Vector(float) constructor causes the problem because it provides an implicit conversion from float to vector. As a basic rule, you should always declare one-arg constructors explicit:

explicit Vector(float f): x(f), y(f), z(f)
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top