I'm porting a class which implements IEquatable<T>
and IComparable<T>
and overrides ==
, !=
, <
and >
from C# into C++/CLI. So far I have:
我正在移植一个实现IEquatable
Header:
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);
Source file:
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
}
I'm not sure why the original tested equality in the interface and compared things in the operator, but I'm trying to preserve the original structure.
我不确定为什么原始测试界面中的相等性并比较运算符中的东西,但我试图保留原始结构。
Anyway, I get a compilation error at the marked line: "error C2679: binary '>' : no operator found which takes a right-hand operand of type 'ThingNamespace::Thing' (or there is no acceptable conversion)
", and a corresponding error two lines below. Why isn't it picking up the existence of the overloaded operator?
无论如何,我在标记的行处得到一个编译错误:“错误C2679:二进制'>':找不到哪个操作符采用'ThingNamespace :: Thing'类型的右手操作数(或者没有可接受的转换)”,以及相应的错误如下两行。为什么它不会存在重载运算符?
3 个解决方案
#1
this
is a pointer, you'll need to dereference it.
这是一个指针,你需要取消引用它。
if ((*this) > other)
return 1;
else if ((*this) < other)
return -1;
else
return 0;
#2
return (*this) == other ? 0 : ((*this) > other ? 1 : -1);
#3
as arul said, you need to dereference the this keyword, but on a side note, you should probably use const references in your function paramaters instead of passing the object since:
正如arul所说,你需要取消引用this关键字,但是在旁注中,你应该在函数参数中使用const引用而不是传递对象,因为:
-C++ passes all objects by value, not by reference(which is what happens in C#), so using references reduces the overhead.
-C ++按值传递所有对象,而不是通过引用传递(这是C#中发生的事情),因此使用引用可以减少开销。
-It'll let you use functions from the standard library such as std::sort without needing to explicitly specify a new comparison operator
- 它允许您使用标准库中的函数,例如std :: sort,而无需显式指定新的比较运算符
#1
this
is a pointer, you'll need to dereference it.
这是一个指针,你需要取消引用它。
if ((*this) > other)
return 1;
else if ((*this) < other)
return -1;
else
return 0;
#2
return (*this) == other ? 0 : ((*this) > other ? 1 : -1);
#3
as arul said, you need to dereference the this keyword, but on a side note, you should probably use const references in your function paramaters instead of passing the object since:
正如arul所说,你需要取消引用this关键字,但是在旁注中,你应该在函数参数中使用const引用而不是传递对象,因为:
-C++ passes all objects by value, not by reference(which is what happens in C#), so using references reduces the overhead.
-C ++按值传递所有对象,而不是通过引用传递(这是C#中发生的事情),因此使用引用可以减少开销。
-It'll let you use functions from the standard library such as std::sort without needing to explicitly specify a new comparison operator
- 它允许您使用标准库中的函数,例如std :: sort,而无需显式指定新的比较运算符