Overloading + on points

Eduardo Cavazos wayo.cavazos at gmail.com
Tue Aug 31 08:01:01 PDT 2010


Hello,

Here's a short program which creates a type for points and overloads '+' 
to do element wise addition as well as addition to floats, however it 
produces an error:

----------------------------------------------------------------------
import std.stdio ;

struct Pt
{
   float x , y ;

   Pt opBinary ( string op ) ( Pt a ) if ( op == "+" )
     { return Pt ( x + a.x , y + a.y ) ; }

   Pt opBinary ( string op ) ( float a ) if ( op == "+" )
     { return Pt ( x + a , y + a ) ; }
}

void main () { Pt ( 1.0 , 2.0 ) + Pt ( 3.0 , 4.0 ) ; }
----------------------------------------------------------------------

The error:

pt_overload_test_a.d(15): Error: template instance opBinary!("+") 
matches more than one template declaration, 
pt_overload_test_a.d(8):opBinary(string op) if (op == "+") and 
pt_overload_test_a.d(11):opBinary(string op) if (op == "+")

So, how should I go about this? :-)

Ed


More information about the Digitalmars-d-learn mailing list