![](/img/trans.png)
[英]C# error CS0246 The type or namespace name 'Socket' could not be found (are you missing a using directive or an assembly reference)
[英]CS0246: The type or namespace name `T' could not be found. Are you missing a using directive or an assembly reference? in c#
我正在从事C#,并且是初学者。 我在创建霍夫曼树的情况下,在其中计算二进制文件中符号的频率(我的意思是符号重复的次数就是频率)。我试图使此“符号”适用于所有数据类型像int
, short
, ulong
等。我使用泛型。
然后我尝试运行代码,我遇到了4个错误,例如:
CS0246: The type or namespace name `T' could not be found. Are you missing a using directive or an assembly reference?
我知道编译器无法识别此“ T
”,但是在“ T
”之前,我仅使用public class Node<K>
而不是public class Node<T> where T : K
因此错误是:
z.cs(13,23): warning CS0693: Type parameter `K' has the same name as the type parameter from outer type `shekhar_final_version_Csharp.Huffman<K>'
z.cs(10,18): (Location of the symbol related to previous warning)
因此,我不得不用等效的“ T
”替换此“ K
”。 但是现在错误变成了4(相似):
z.cs(20,21): error CS0246: The type or namespace name `T' could not be found. Are you missing a using directive or an assembly reference?
z.cs(72,38): error CS0246: The type or namespace name `T' could not be found. Are you missing a using directive or an assembly reference?
z.cs(83,21): error CS0246: The type or namespace name `T' could not be found. Are you missing a using directive or an assembly reference?
z.cs(267,40): error CS0246: The type or namespace name `T' could not be found. Are you missing a using directive or an assembly reference?
我的完整代码是:
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
public class Huffman<K> where K : IComparable<K>
{
public int data_size, length, i, is_there;
Line 13: public class Node<T> where T : K
{
public Node<T> next, left, right;
public K symbol;
public int freq;
public int is_processed;
}
Line 20: public Node<T> front, rear;
///////////////////////////////////////////////
public Huffman(string[] args)
{
front = null;
rear = null;
using(var stream = new BinaryReader(System.IO.File.OpenRead(args[0])))
{
while (stream.BaseStream.Position < stream.BaseStream.Length)
{
int processingValue = stream.ReadByte();
{
Node<T> pt, temp;
bool is_there = false;
pt = front;
while (pt != null)
{
if (pt.symbol == processingValue)
{
pt.freq++;
is_there = true;
break;
}
temp = pt;
pt = pt.next;
}
if (is_there == false)
{
temp = new Node<T>();
temp.symbol = processingValue;
temp.freq = 1;
temp.left = null;
temp.right = null;
temp.next = null;
temp.is_processed = 0;
if (front == null)
{
front = temp;
}
else
{
temp.next = front;
front = temp;
}
}
}
}
stream.Close();
//////////////////////////////
}
}
}
有人可以帮我消除这些错误吗? 我真的很感激。 但是请记住,如果我在第13行中执行“ public class Node<K>
”而不是“ public class Node<T> where T : K
”,则会出现以下错误:
z.cs(13,23): warning CS0693: Type parameter `K' has the same name as the type parameter from outer type `shekhar_final_version_Csharp.Huffman<K>'
z.cs(10,18): (Location of the symbol related to previous warning)
让我们看一下这个的简化版本:
public class Huffman<K>
{
public class Node<T> where T : K
{
}
public Node<T> front, rear;
}
是什么类型front
和rear
在这里? 它指的是T
,但我们不知道T
是什么。 T
是Node<T>
的类型参数 ,但是当您声明字段时,需要提供type 参数 。
我怀疑您实际上想要的是:
public Node<K> front, rear;
坦白说,目前尚不清楚您是否需要将Node
完全成为通用类。 我怀疑您可能会满意:
public class Huffman<K>
{
public class Node
{
// You can still use K here...
}
public Node front, rear;
}
考虑一下您是否真的需要两个不同的类型参数-这样做有什么好处?
除了Jon Skeet的回答外,在方法中使用泛型类型时,还需要将泛型声明添加到方法中:
例如:
public void Print_tree(Node<T> treee)
成为:
public void Print_tree<T>(Node<T> treee)
和
public Node<T> find_two_smallest(ref Node<T> pmin1, ref Node<T> pmin2)
成为:
public Node<T> find_two_smallest<T>(ref Node<T> pmin1, ref Node<T> pmin2)
即使您不在参数中而是在方法的主体中使用它,方法声明也需要知道它,您的方法huffman_node_processing()在主体中使用Node<T>
。 因此,Visual Studio告诉您它需要在方法声明中知道它。 通过将声明更改为
huffman_node_processing<T>()您告诉C#该方法使用类型
T
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.