struct opCmp confustion
Era Scarecrow
rtcvb32 at yahoo.com
Sun Aug 26 10:14:47 PDT 2012
On Sunday, 26 August 2012 at 16:27:32 UTC, Charles Hixson wrote:
> Currently the code is:
>
> struct Triplet
> { string wrd1;
> string wrd2;
> string wrd3;
> int val = 1;
>
> int opCmp(ref const Triplet t) const
> { if (wrd1 < t.wrd1) return -1;
> if (wrd1 > t.wrd1) return 1;
> if (wrd2 < t.wrd2) return -1;
> if (wrd2 > t.wrd2) return 1;
> if (wrd3 < t.wrd3) return -1;
> if (wrd3 > t.wrd3) return 1;
> return 0;
> //auto i = wrd1.opCmp (t.wrd1);
> //if (i != 0) return i;
> //i = wrd2.opCmp (t.wrd2);
> //if (i != 0) return i;
> //return wrd3.opCmp (t.wrd3);
> }
> }
> The commented out code was the original version, which wouldn't
> compile.
> (The current code replaces it.) It gave messages like:
> ...ser$ rdmd --main -unittest -DdDocs parse1.d
> parse1.d(114): Error: undefined identifier 'opCmp'
> parse1.d(116): Error: undefined identifier 'opCmp'
> parse1.d(118): Error: undefined identifier 'opCmp'
>
> What was I doing wrong?
I would think it's you attempting to compare a string. Try using
cmp?.
untested:
import std.algorithm : cmp;
struct Triplet {
string wrd1, wrd2, wrd3;
int opCmp(ref const Triplet t) const {
int result1 = cmp(wrd1, t.wrd1);
int result2 = cmp(wrd2, t.wrd2);
int result3 = cmp(wrd3, t.wrd3);
if (result1) return result1;
if (result2) return result2;
if (result3) return result3;
return 0;
}
}
More information about the Digitalmars-d-learn
mailing list