简体   繁体   English

C#中的比较:operator'<'不能应用于'T'和'T'类型的操作数

[英]Comparison in C#: operator '<' cannot be applied to operands of type 'T' and 'T'

I have created a BinaryTreeNode<T> class and then creating Add(T data) method for BinaryTree<T> class. 我已经创建了一个BinaryTreeNode<T>类,然后创建Add(T data)用于方法BinaryTree<T>类。

When I try to compare Values of objects compiler says: 当我尝试比较对象的值时编译器说:

operator '<' cannot be applied to operands of type 'T' and 'T' . operator'<'不能应用于'T'和'T'类型的操作数

Example: 例:

  public void AddNode(T data) {
        BinaryTreeNode<T> node = new BinaryTreeNode<T>(data);
        BinaryTreeNode<T> temp = root;

        if (temp.Value < node.Value) // **PROBLEM HERE**
        ...

I'm using VS08 Express Edition. 我正在使用VS08 Express Edition。

You should add a constraint such that T must implement IComparable<T> and then use that: 您应该添加一个约束,以便T必须实现IComparable<T> ,然后使用它:

public class BinaryTree<T> where T : IComparable<T>
{
    public void AddNode(T data)
    {
        BinaryTreeNode<T> node = new BinaryTreeNode<T>(data);
        BinaryTreeNode<T> temp = root;

        if (temp.Value.CompareTo(node.Value) < 0)
        ...

An alternative is to pass in an IComparer<T> and use that: 另一种方法是传入IComparer<T>并使用它:

public class BinaryTree<T> where T : IComparable<T>
{
    private readonly IComparer<T> comparer;

    public BinaryTree(IComparer<T> comparer)
    {
        this.comparer = comparer;
        ...
    }

    public void AddNode(T data)
    {
        BinaryTreeNode<T> node = new BinaryTreeNode<T>(data);
        BinaryTreeNode<T> temp = root;

        if (comparer.Compare(temp.Value, node.Value) < 0)

This is the closest you can get to guaranteeing a "<" operator - overloaded operators are static, and there's no way of constraining a type argument to require it. 这是保证“<”运算符最接近的 - 重载运算符是静态的,并且没有办法约束类型参数来要求它。

Look into using Generic Constraints to narrow down the types that T can be. 研究使用Generic Constraints来缩小T可以的类型。 That way you can ensure that they can be compared using your operator. 这样您就可以确保使用您的运营商进行比较。

At the moment T could be any object. 此刻,T可能是任何对象。 For instance, if you had a Car object, how would the compiler know what to make of saying one Car is "less than" or "greater than" another? 例如,如果你有一个Car对象,那么编译器如何知道如何说一辆Car是“小于”还是“大于”另一辆? That is why you need constraints. 这就是你需要约束的原因。

The type (int, string, char[], MyClass, YourClass, etc) of Value doesn't support the '<' operation. Value的类型(int,string,char [],MyClass,YourClass等)不支持'<'操作。 That is normal for most non-intrinsic number types, ie int, long, decimal, etc. 这对于大多数非内在数字类型来说是正常的,即int,long,decimal等。

T needs to implement the IComparable class so it can be compared with other object of type T. T需要实现IComparable类,以便可以与T类型的其他对象进行比较。

So, you function declaration must enforce a constraint on T: 因此,函数声明必须对T强制执行约束:

public class BinaryTree<T> where T : IComparable<T>
{
    public void AddNode(T data) 

and you must make sure that whatever T is, it must implement IComparable. 并且你必须确保无论T是什么,它必须实现IComparable。

public class MyData : IComparable<MyData>
{
    public int CompareTo(MyData other)
    {
         // return -1 if 'this' is smaller than other, 0 if equals, 1 otherwise
    }
}

In your add function, you then call 在你的添加功能中,然后调用

if( temp.Value.CompareTo(node.Value) < 0 )

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 接线员'??'不能应用于'T'和'T'类型的操作数 - Operator '??' cannot be applied to operands of type 'T' and 'T' 运算符'=='不能应用于'T'和'T'类型的操作数 - Operator '==' cannot be applied to operands of type 'T' and 'T' 运算符“==”不能应用于“T”和“int”类型的操作数 - Operator '==' cannot be applied to operands of type 'T' and 'int' c#-Linq错误“运算符&#39;==&#39;无法应用于类型为&#39;int&#39;和&#39;System.Linq.IQueryable的操作数 <T> ”和RemoveRange - c# - Linq Error “Operator '==' cannot be applied to operands of type 'int' and 'System.Linq.IQueryable<T>” and RemoveRange 痛苦的泛型,运算符&#39;&gt; =&#39;不能应用于&#39;T&#39;和&#39;T&#39;类型的操作数 - Painful Generics, Operator '>=' cannot be applied to operands of type 'T' and 'T' C#运算符&#39;*&#39;不能应用于类型&#39;double&#39;和&#39;decimal&#39;的操作数 - C# Operator '*' cannot be applied to operands of type 'double' and 'decimal' C# 错误运算符 * 不能应用于“字符串”和“字符串”类型的操作数 - C# ERROR Operator * cannot be applied to operands of type 'string' and 'string' C#运算符&#39;/&#39;不能应用于类型为&#39;Vector3&#39;和&#39;float&#39;的操作数 - C# Operator '/' cannot be applied to operands of type 'Vector3' and 'float' C#:运算符&#39;!=&#39;不能应用于类型&#39;object&#39;和&#39;char&#39;的操作数 - C#: Operator '!=' cannot be applied to operands of type 'object' and 'char' C#运算符&#39;+&#39;不能应用于类型&#39;IntPtr&#39;和&#39;int&#39;的操作数 - C# Operator '+' cannot be applied to operands of type 'IntPtr' and 'int'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM