[Noob] Operator overloading - am I missing something?

Hasan Aljudy hasan.aljudy at gmail.com
Mon May 8 07:58:31 PDT 2006


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