오류 CS0311 'HashTables.KeyValue <K, V>'형식은 제네릭 형식 또는 메서드 'BinarySearchTree'에서 형식 매개 변수 'Y'로 사용할 수 없습니다. 'HashTables.KeyValue <K, V>'에서 'System.IComparable <HashTables.KeyValue <K, V >>'로의 암시 적 참조 변환은 없습니다.
사용자 정의 KeyValue 클래스
public class KeyValue<K,V> where K : IComparable<K> where V : IComparable<V>
{
K key;
V value;
public KeyValue(K key, V value)
{
key = key;
value = value;
}
public K Key
{
get
{
return key;
}
}
public V Value
{
get
{
return value;
}
}
}
KeyValue를 약간 변경하면됩니다. 문제는 KeyValue가 실제로 트리에서 비교하려는 요소라는 것입니다 (대부분 값으로). 명확성을 위해 Null 검사가 생략되었습니다.
public class KeyValue<K, V> : IComparable<KeyValue<K, V>>
where K : IComparable<K>
where V : IComparable<V>
{
//Store the key and the value
K kKey;
V vValue;
public int CompareTo([AllowNull] KeyValue<K, V> other)
{
return this.Value.CompareTo(other.Value);
}
//Constructor
public KeyValue(K key, V value)
{
kKey = key;
vValue = value;
}
public K Key
{
get
{
return kKey;
}
}
public V Value
{
get
{
return vValue;
}
}
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다