www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Difficulty with operator overloading opMulAssign

reply Spacen Jasset <spacen yahoo.co.uk> writes:
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
parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"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
parent Spacen Jasset <spacenjasset yahoo.co.uk> writes:
Jarrett Billingsley wrote:
 "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; :)
That's something I didn't try but should of. I didn't think 'this' was a pointer type proper. Thanks for the fix :-)
Mar 03 2008