www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - [Noob] Operator overloading - am I missing something?

reply Anders Runesson <anders runesson.info> writes:
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
May 08 2006
parent reply Hasan Aljudy <hasan.aljudy gmail.com> writes:
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
 
 
May 08 2006
parent Anders Runesson <anders runesson.info> writes:
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
 
May 08 2006