Je suis le portage d'une classe qui implémente IEquatable<T>
et IComparable<T>
et remplace ==
, !=
, <
et >
de C# en C++/CLI. Jusqu'à présent, j'ai:Comment remplacer < and > en C++/CLI?
tête:
virtual bool Equals(Thing other);
virtual int CompareTo(Thing other);
static bool operator == (Thing tc1, Thing tc2);
static bool operator != (Thing tc1, Thing tc2);
static bool operator > (Thing tc1, Thing tc2);
static bool operator < (Thing tc1, Thing tc2);
Fichier source:
bool Thing ::Equals(Thing other)
{
// tests equality here
}
int Thing ::CompareTo(Thing other)
{
if (this > other) // Error here
return 1;
else if (this < other)
return -1;
else
return 0;
}
bool Thing ::operator == (Thing t1, Thing t2)
{
return tc1.Equals(tc2);
}
bool Thing ::operator != (Thing t1, Thing t2)
{
return !tc1.Equals(tc2);
}
bool Thing::operator > (Thing t1, Thing t2)
{
// test for greater than
}
bool Thing::operator < (Thing t1, Thing t2)
{
// test for less than
}
Je ne sais pas pourquoi l'égalité testé d'origine dans l'interface et les choses par rapport à l'opérateur, mais je Essaie de préserver la structure originale.
De toute façon, j'obtiens une erreur de compilation à la ligne marquée: "error C2679: binary '>' : no operator found which takes a right-hand operand of type 'ThingNamespace::Thing' (or there is no acceptable conversion)
", et une erreur correspondante deux lignes ci-dessous. Pourquoi ne prend-il pas conscience de l'existence de l'opérateur surchargé?