www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - operator overloading compilation error

reply =?ISO-8859-1?Q?Diego_Canuh=E9?= <canuhedc gmail.com> writes:
Hi
I'm trying to overload the "+" and "-" operators for a struct  but I get
this error

test.d(47): Error: incompatible types for ((v1) - (v2)): 'Vecf!(1u)' and
'Vecf!(1u)'
(the line corresponds to the last assert)

doing something like this

struct Vecf(uint n)
{
    ...
    Vecf!n opBinary(string op)(Vecf!n other) if (op == "+" || op == "-")
{...}
}

void main()
{
    Vecf!1u v1 = Vecf!1u(3f);
    Vecf!1u v2 = Vecf!1u(5f);
    Vecf!1u r = Vecf!1u(-2f);
    assert ((v1.opBinary!"-"(v2)) == r);
    assert ((v1 - v2) == r);
}

removing the last assert or replacing (Vecf!n other) by (Vecf!1u other) in
the function declaration works fine, but it's not what I want

Any ideas? maybe I'm overloading the operators in the wrong way (it worked
in a very similar test though)?

Thanks!!

Here's the full code:

-------------------------------------------------------------------
module test;

import std.stdio;

struct Vecf(uint n)
{
    float[n] data;


    this(float[n] args ...)
    {
        foreach (i, a; args)
        {
            data[i] = a;
        }
    }

    float opIndex(uint i)
    {
        return data[i];
    }

    float opIndexAssign(float value, uint i)
    {
        data[i] = value;
        return value;
    }

    Vecf!n opBinary(string op)(Vecf!n other) if (op == "+" || op == "-")
    {
        Vecf!n ret;

        for (size_t i = 0; i < n; i++)
        {
            mixin("ret[i] = this[i] " ~ op ~ " other[i];");
        }
        return ret;
    }
}

void main()
{
    Vecf!1u v1 = Vecf!1u(3f);
    Vecf!1u v2 = Vecf!1u(5f);
    Vecf!1u r = Vecf!1u(-2f);
    assert ((v1.opBinary!"-"(v2)) == r);
    assert ((v1 - v2) == r);
}
--------------------------------------------------------------------------------------
Jul 26 2011
parent reply bearophile <bearophileHUGS lycos.com> writes:
Diego Canuhé:

 I'm trying to overload the "+" and "-" operators for a struct  but I get
 this error
 
 test.d(47): Error: incompatible types for ((v1) - (v2)): 'Vecf!(1u)' and
 'Vecf!(1u)'
 (the line corresponds to the last assert)
It's a dmd bug, and I think it's already in Bugzilla. This is workaround code: struct Vecf(uint n) { float x; Vecf opBinary(string op)(Vecf other) if (op == "+" || op == "-") { mixin("return Vecf(this.x " ~ op ~ " other.x);"); } } void main() { alias Vecf!1 V; auto v1 = V(3f); auto v2 = V(5f); auto r = V(-2f); assert ((v1.opBinary!"-"(v2)) == r); // better to use an approximate == assert ((v1 - v2) == r); // better to use an approximate == } Bye, bearophile
Jul 26 2011
parent =?ISO-8859-1?Q?Diego_Canuh=E9?= <canuhedc gmail.com> writes:
thanks!

On Tue, Jul 26, 2011 at 9:12 AM, bearophile <bearophileHUGS lycos.com>wrote=
:

 Diego Canuh=E9:

 I'm trying to overload the "+" and "-" operators for a struct  but I ge=
t
 this error

 test.d(47): Error: incompatible types for ((v1) - (v2)): 'Vecf!(1u)' an=
d
 'Vecf!(1u)'
 (the line corresponds to the last assert)
It's a dmd bug, and I think it's already in Bugzilla. This is workaround code: struct Vecf(uint n) { float x; Vecf opBinary(string op)(Vecf other) if (op =3D=3D "+" || op =3D=3D "-=
") {
        mixin("return Vecf(this.x " ~ op ~ " other.x);");
    }
 }

 void main() {
    alias Vecf!1 V;
    auto v1 =3D V(3f);
    auto v2 =3D V(5f);
    auto r =3D V(-2f);
    assert ((v1.opBinary!"-"(v2)) =3D=3D r); // better to use an approxima=
te =3D=3D
    assert ((v1 - v2) =3D=3D r); // better to use an approximate =3D=3D
 }

 Bye,
 bearophile
Jul 26 2011