Thread overview | |||||
---|---|---|---|---|---|
|
March 03, 2008 Difficulty with operator overloading opMulAssign | ||||
---|---|---|---|---|
| ||||
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 Re: Difficulty with operator overloading opMulAssign | ||||
---|---|---|---|---|
| ||||
Posted in reply to Spacen Jasset | "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 Re: Difficulty with operator overloading opMulAssign | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jarrett Billingsley | 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 :-)
|
Copyright © 1999-2021 by the D Language Foundation