2017-12-21 4 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.

어떻게 LiteHeap을 제네릭 클래스 T impl을 사용하는 클래스로 만들 수 있습니까? 에서 IComparable 그래서 난 new LiteHeap<SomeClass>을 쓸 수 ementing 및 SomeClass가에서 IComparable

+1

는'IComparable'은'에서 IComparable는 ' – Mahmoud

답변

5

IComparableIComparable<T> 다른, 완전히 무관 한 인터페이스입니다을 구현하는 곳 작동합니다.

으로 변경해야 클래스와 실제로 일치해야합니다.

+2

... 또는 클래스가'IComparable' 모두 구현이 아닌'에서 IComparable '그것은 당신이'IEnumerable'을 처리 같은 방식으로 구현하고'IEnumerable을 ' 의 구현. –

관련 문제