I have a vector class with a properly overloaded Vect*float operator and am trying to create the global/non-member float*Vect operator as follows: (Note this is a heavily edited sample)
class Vect
{
public:
Vect::Vect(const float p_x, const float p_y, const float p_z, const float p_w);
Vect operator*(const float p_sclr) const;
private:
float x;
float y;
float z;
float w;
};
Vect::Vect(const float p_x, const float p_y, const float p_z, const float p_w) {
x = p_x;
y = p_y;
z = p_z;
w = p_w;
}
Vect Vect::operator*(const float p_sclr) const {
return Vect( (x * p_sclr), (y * p_sclr), (z * p_sclr), 1); // reset w to 1
}
//Problem Non-MemberOperator
Vect operator*(const float p_sclr, const Vect& p_vect);
Vect operator*(const float p_sclr, const Vect& p_vect) {
return p_vect * p_sclr;
}
But when I go to test the operator with the call:
Vect A(2.0f, 3.0f, 4.0f, 5.0f);
float s = 5.0f;
Vect C, D;
C = A * s; // Fine
D = s * A; // Error as below
I receive the following compile error:
error C2678: binary '*' : no operator found which takes a left-hand operand of type 'float' (or there is no acceptable conversion)
Can anyone provide insight to why this happens? The MS documentation is available at http://msdn.microsoft.com/en-us/library/ys0bw32s(v=VS.90).aspx and isn't very helpful Visual Studio 2008. This is the only compile error or warning I receive.