所以我正在滚动我自己的最大堆,并且我使用扩展接口的泛型类做错了。说我有这样一个类:
class SuffixOverlap:IComparable<SuffixOverlap>
{
//other code for the class
public int CompareTo(SuffixOverlap other)
{
return SomeProperty.CompareTo(other.SomeProperty);
}
}
然后我创建我的堆类:
class LiteHeap<T> where T:IComparable
{
T[] HeapArray;
int HeapSize = 0;
public LiteHeap(List<T> vals)
{
HeapArray = new T[vals.Count()];
foreach(var val in vals)
{
insert(val);
}
}
//the usual max heap methods
}
但是当我尝试这样做时:
LiteHeap<SuffixOverlap> olHeap = new LiteHeap<SuffixOverlap>(listOfSuffixOverlaps);
我收到错误:The type SuffixOverlap cannot be used as a type parameter T in the generic type or method LiteHeap<T>. There is no implicit reference conversion from SuffixOverlap to System.IComparable.
我如何创建LiteHeap作为使用通用类T实现IComparable的类,所以我可以编写new LiteHeap<SomeClass>
,它将在SomeClass实现IComparable的地方工作
IComparable
和IComparable<T>
是不同的,完全不相关的接口。
您需要将其更改为where T : IComparable<T>
,以便它实际匹配您的班级。