C# 错误CS0311 Visual Studio中的一般错误
错误CS0311类型“HashTables.KeyValue”不能用作泛型类型或方法“BinarySearchTree”中的类型参数“Y”。没有从“HashTables.KeyValue”到“System.IComparable”的隐式引用转换 自定义键值类C# 错误CS0311 Visual Studio中的一般错误,c#,visual-studio,compiler-errors,C#,Visual Studio,Compiler Errors,错误CS0311类型“HashTables.KeyValue”不能用作泛型类型或方法“BinarySearchTree”中的类型参数“Y”。没有从“HashTables.KeyValue”到“System.IComparable”的隐式引用转换 自定义键值类 public class KeyValue<K,V> where K : IComparable<K> where V : IComparable<V> {
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,其中K:IComparable,其中V:IComparable
{
K键;
V值;
公钥值(K键,V值)
{
钥匙=钥匙;
价值=价值;
}
公钥
{
得到
{
返回键;
}
}
公共价值
{
得到
{
返回值;
}
}
}
您只需要稍微更改一下您的键值。问题在于,KeyValue实际上是要在树中比较的元素(很可能是按值)。为清晰起见,省略了空检查
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;
}
}
}
公共类KeyValue:IComparable
其中K:i可比较
其中V:i可比较
{
//存储键和值
K kKey;
V值;
公共整数比较([AllowNull]键值其他)
{
返回此.Value.CompareTo(其他.Value);
}
//建造师
公钥值(K键,V值)
{
kKey=key;
v值=值;
}
公钥
{
得到
{
返回kKey;
}
}
公共价值
{
得到
{
返回v值;
}
}
}
您只需要稍微更改一下您的键值。问题在于,KeyValue实际上是要在树中比较的元素(很可能是按值)。为清晰起见,省略了空检查
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;
}
}
}
公共类KeyValue:IComparable
其中K:i可比较
其中V:i可比较
{
//存储键和值
K kKey;
V值;
公共整数比较([AllowNull]键值其他)
{
返回此.Value.CompareTo(其他.Value);
}
//建造师
公钥值(K键,V值)
{
kKey=key;
v值=值;
}
公钥
{
得到
{
返回kKey;
}
}
公共价值
{
得到
{
返回v值;
}
}
}
查看此错误的文档:什么是KeyValue
?鉴于它显然没有实现IComparable
,为什么您认为应该能够将其用作BST
的类型参数?当询问有关堆栈溢出错误的问题时,要求您以文本形式提供完整的错误消息内容。有关预期内容的详细信息,请阅读。错误说明KeyValue
未实现IComparable
。您只对KeyValue
的泛型类型施加限制,而不是对整个类型施加限制。这会触发错误请查看此错误的文档:什么是KeyValue
?鉴于它显然没有实现IComparable
,为什么您认为应该能够将其用作BST
的类型参数?当询问有关堆栈溢出错误的问题时,要求您以文本形式提供完整的错误消息内容。有关预期内容的详细信息,请阅读。错误说明KeyValue
未实现IComparable
。您只对KeyValue
的泛型类型施加限制,而不是对整个类型施加限制。这会引发一个错误