[Noob] Operator overloading - am I missing something?
Anders Runesson
anders at runesson.info
Mon May 8 08:00:59 PDT 2006
Oh, man do I feel stupid now... :)
Thanks a lot
/Anders
Den Mon, 08 May 2006 08:58:31 -0600 skrev Hasan Aljudy:
> it's opMul not opMult
>
> Anders Runesson wrote:
>> Hi there.
>> I'm converting some old C code to D, but I'm stumbling on this:
>>
>> ...
>>
>> alias GLdouble Scalar;
>>
>> ...
>>
>> class Vector {
>> private Scalar[4] data;
>>
>> ...
>>
>> Vector opMult(Scalar s) {
>>
>> ...
>>
>> }
>>
>> Vector opDiv(Scalar s) {
>>
>> return this * (1.0 / s); // Doesn't work
>>
>> //return this.opMult(1.0 / s); Works as I expected
>> }
>>
>> ...
>>
>> }
>>
>> I am trying to overload the division operator, but I can't use the
>> multiplication operator I just overloaded(or at least thought I did).
>> The compiler says
>>
>> vector.d(112): incompatible types for ((this) * (1 / cast(double)(s))): \
>> 'bagle.math.vector.Vector' and 'double'
>>
>> vector.d(112): 'this' is not an arithmetic type
>>
>> and refuses to compile the code. The commented out line works fine.
>> Why is this? I thought this.opMult(s) and this * s were synonymous?
>>
>> I suppose I'm missing something, but from just reading the spec I couldn't
>> figure it out.
>>
>> Thanks
>> /Anders
>>
>>
More information about the Digitalmars-d-learn
mailing list