Hi<br>I'm trying to overload the "+" and "-" operators for a struct but I get this error<br><br>test.d(47): Error: incompatible types for ((v1) - (v2)): 'Vecf!(1u)' and 'Vecf!(1u)'<br>
(the line corresponds to the last assert)<br><br>doing something like this<br><br>struct Vecf(uint n)<br>
{<br> ...<br> Vecf!n opBinary(string op)(Vecf!n other) if (op == "+" || op == "-") {...}<br>}<br><br>void main()<br>{<br> Vecf!1u v1 = Vecf!1u(3f);<br>
Vecf!1u v2 = Vecf!1u(5f);<br>
Vecf!1u r = Vecf!1u(-2f);<br>
assert ((v1.opBinary!"-"(v2)) == r);<br>
assert ((v1 - v2) == r);<br>}<br><br>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<br><br>Any ideas? maybe I'm overloading the operators in the wrong way (it worked in a very similar test though)?<br>
<br>Thanks!!<br><br>Here's the full code:<br><br>-------------------------------------------------------------------<br>module test;<br><br>import std.stdio;<br><br>struct Vecf(uint n)<br>{<br> float[n] data;<br> <br>
<br> this(float[n] args ...)<br> {<br> foreach (i, a; args)<br> {<br> data[i] = a;<br> }<br> }<br> <br> float opIndex(uint i)<br> {<br> return data[i];<br> }<br>
<br> float opIndexAssign(float value, uint i)<br> {<br> data[i] = value;<br> return value;<br> }<br> <br> Vecf!n opBinary(string op)(Vecf!n other) if (op == "+" || op == "-")<br>
{<br> Vecf!n ret;<br> <br> for (size_t i = 0; i < n; i++)<br> {<br> mixin("ret[i] = this[i] " ~ op ~ " other[i];");<br> }<br> return ret;<br>
}<br>}<br><br>void main()<br>{<br> Vecf!1u v1 = Vecf!1u(3f);<br> Vecf!1u v2 = Vecf!1u(5f);<br> Vecf!1u r = Vecf!1u(-2f);<br> assert ((v1.opBinary!"-"(v2)) == r);<br> assert ((v1 - v2) == r);<br>
}<br>--------------------------------------------------------------------------------------<br><br><br>