digitalmars.D.learn - Difficulty with operator overloading opMulAssign
- Spacen Jasset (42/42) Mar 02 2008 DMD 1 reports an error for the struct below, marked "// Error":
- Jarrett Billingsley (5/19) Mar 02 2008 Try
- Spacen Jasset (3/29) Mar 03 2008 That's something I didn't try but should of. I didn't think 'this' was a...
DMD 1 reports an error for the struct below, marked "// Error": math/vector.d(35): Error: incompatible types for ((this) *= cast(float)1 / m)): 'Vector3 *' and 'float' math/vector.d(35): Error: 'this' is not of arithmetic type, it is a Vector3 * What is it that I am doing wrong here? struct Vector3 { float x, y, z; float magnitude() { return sqrt(squareMagnitude()); } float squareMagnitude() { return x*x + y*y + z*z; } void scalarMultiply(float scalar) { x *= scalar; y *= scalar; z *= scalar; } void opMulAssign(float scalar) { scalarMultiply(scalar); } void normalise() { float m = magnitude(); if (m) { this *= 1 / m; // Error } } } Regards, Jason
Mar 02 2008
"Spacen Jasset" <spacen yahoo.co.uk> wrote in message news:fqfi8l$haj$1 digitalmars.com...DMD 1 reports an error for the struct below, marked "// Error": math/vector.d(35): Error: incompatible types for ((this) *= cast(float)1 / m)): 'Vector3 *' and 'float' math/vector.d(35): Error: 'this' is not of arithmetic type, it is a Vector3 * What is it that I am doing wrong here? ... void normalise() { float m = magnitude(); if (m) { this *= 1 / m; // Error } }Try (*this) *= 1 / m; :)
Mar 02 2008
Jarrett Billingsley wrote:"Spacen Jasset" <spacen yahoo.co.uk> wrote in message news:fqfi8l$haj$1 digitalmars.com...That's something I didn't try but should of. I didn't think 'this' was a pointer type proper. Thanks for the fix :-)DMD 1 reports an error for the struct below, marked "// Error": math/vector.d(35): Error: incompatible types for ((this) *= cast(float)1 / m)): 'Vector3 *' and 'float' math/vector.d(35): Error: 'this' is not of arithmetic type, it is a Vector3 * What is it that I am doing wrong here? ... void normalise() { float m = magnitude(); if (m) { this *= 1 / m; // Error } }Try (*this) *= 1 / m; :)
Mar 03 2008