";int”;至“;通用的“;数据类型在c#中给出错误

本文关键字:错误 出错 数据类型 int quot | 更新日期: 2023-09-27 17:51:02

我是c#初学者,创建了适用于"int"类型符号的huffman tree,但下一步是使其成为generic。在解释中,symbol应该适用于每个data type我的意思是,每个数据类型的符号都可以是int、ulong、short等类型。实际上,我正在读取一个binary file,并试图找到每个符号重复的频率(符号重复的次数就是它的频率(,所以我的节点中的这个符号应该在32/64位体系结构上适用于"short"、"long"answers"unsigned"。目前,我在代码中使用了"int",但它可能是"short"或任何其他的

我已经尝试过了,但我有错误,下面给出了我的代码,它包含有错误的行号。错误是:获得的误差为:

hp@ubuntu:~/Desktop/Internship_Xav/templatescplus$ gmcs z.cs 
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)
z.cs(37,43): error CS0019: Operator `==' cannot be applied to operands of type `K' and `int'
z.cs(50,41): error CS0029: Cannot implicitly convert type `int' to `K'
z.cs(283,21): error CS0246: The type or namespace name `K' could not be found. Are you missing a using directive or an assembly reference?
z.cs(285,13): error CS0841: A local variable `ObjSym' cannot be used before it is declared
z.cs(286,13): error CS0841: A local variable `ObjSym' cannot be used before it is declared
z.cs(288,13): error CS0841: A local variable `ObjSym' cannot be used before it is declared
Compilation failed: 6 error(s), 1 warnings
hp@ubuntu:~/Desktop/Internship_Xav/templatescplus$ 

代码是:

        namespace final_version_Csharp
         {
    Line 10: public class Huffman<K> where K :  IComparable<K>
            {
                public int data_size, length, i, is_there;
    Line 13:        public class Node<K> 
                {
                    public Node<K> next, left, right;
                    public K symbol;
                    public int freq;
                    public int is_processed;
                }
                public Node<K> 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) 
                        {
Line 37:                        int processingValue = stream.ReadByte(); 
                            {
                                Node<K> 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<K>();
Line 50:                            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();
                        //////////////////////////////
                    }
                }
        ..................................
               public class MyClass 
            {
                public static void Main(string[] args) 
                {
    Line 283:       Huffman<K>  ObjSym = new Huffman<K>(args); //object creation
                    Console.WriteLine("'nReading the Binary file......");
    Line 285:       ObjSym.Print_tree(ObjSym.front);
    Line 286:       ObjSym.huffman_node_processing();
                    Console.WriteLine("'nThe encoding of symbols are :");
    Line 288:       ObjSym.GenerateCode(ObjSym.rear, "");
                }
            }
        }

有人能帮我删除这些错误吗?让这个"符号"适用于所有数据类型,如"短"、"长"等。

";int”;至“;通用的“;数据类型在c#中给出错误

这里至少有两个问题。

  • 您的程序中没有类型Node;对Node的引用需要改为对Node<K>的引用
  • 一旦解决了上一项,您会发现您在几个地方假设您将始终拥有Node<int>,但事实并非如此。您需要弄清楚如何将ReadByte()的输出放入K,以及如何将其与pt.symbol进行比较
  • 编译器不希望在嵌套类定义中使用相同的占位符类型。尝试class Node<T> where T : K而不是class Node<K>
  • Main()的第一行不能使用占位符类型。它需要是树将存储的数据类型
相关文章: