Thread overview
Difficulty with operator overloading opMulAssign
Mar 03, 2008
Spacen Jasset
Mar 03, 2008
Spacen Jasset
March 03, 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
March 03, 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;

:)


March 03, 2008
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 :-)