痛苦的泛型,运算符“> ='不能应用于类型为'T'和'T'的操作数 [英] Painful Generics, Operator '>=' cannot be applied to operands of type 'T' and '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> =数据)
我收到错误:
运算符'> ='不能应用于类型的操作数'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的< T>
,或(2)使用 的IComparer< T>
或仿函数到类。
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;
}
//...
}
这篇关于痛苦的泛型,运算符“> ='不能应用于类型为'T'和'T'的操作数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文