2017-12-21 10 views
0

私は自分の最大ヒープを展開しています。インターフェイスを拡張するジェネリッククラスを使用すると何か問題があります。ジェネリックスの拡張インターフェイスを使用するクラス

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.

ジェネリッククラスのT implを使用するクラスとしてLiteHeapを作成するにはどうすればよいですか私はnew LiteHeap<SomeClass>を書くことができますし、工assがIComparableを

+1

'IComparable'ではない' IComparableを ' – Mahmoud

答えて

5

IComparableIComparable<T>を実装している、それが動作しますIComparableをしementingすることは、完全に無関係なインターフェース異なっています。

where T : IComparable<T>に変更する必要があります。その結果、実際にあなたのクラスに一致します。

+2

...またはクラスは' IComparable'と 'IComparableをの両方を実装し'と、それをあなたが 'IEnumerable'と' IEnumerableをを扱うのと同じ方法を実装してい 'の実装。 –

関連する問題