痛苦的泛型,运算符“> ='不能应用于类型为'T'和'T'的操作数

痛苦的泛型,运算符“> ='不能应用于类型为'T'和'T'的操作数

问题描述:

我在C#中新所以请原谅,这是我的代码:

I'm new in C# so please bear with me , Here is my code:

class BinaryTree<T> 
{
    private node<T> Head;
    public class node<T> 
    {
     public T Data;
     public node<T> right;
     public node<T> left;
     public node<T> parent;
    ...
    }
    ...
    private void insert(ref T data, node<T> parent, ref node<T> currentChild) 
    {
    ...
        {
            if (currentChild.Data >= data) insert(ref data, currentChild, ref currentChild.right);
            else insert(ref data, currentChild, ref currentChild.left);
        }
     }
}



以上点如果(currentChild.Data&GT; =数据)我收到错误:

运算符'> ='不能应用于类型的操作数'T'和'T'

Operator '>=' cannot be applied to operands of type 'T' and 'T'

我该怎么办拍摄的错误?谢谢

What do I do to shoot the error?, Thanks

经典的解决问题的对策是:(1),使 T IComparable的&LT; T&GT; ,或(2)使用 的IComparer&LT; T&GT; 或仿函数到类。

The classic solutions to this problem are (1) to make T IComparable<T>, or (2) to use an IComparer<T> or a functor to your class.

(1)

class BinaryTree<T> where T : Comparable<T> ...



(2)

(2)

class BinaryTree<T> {
    private node<T> Head;
    private readonly IComparer<T> comparer;
    public BinaryTree(IComparer<T> comparer) {
        this.comparer = comparer;
    }
    //...
}