Difficulty with operator overloading opMulAssign

Spacen Jasset spacen at yahoo.co.uk
Sun Mar 2 16:54:46 PST 2008


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


More information about the Digitalmars-d-learn mailing list