“int”到“generic”数据类型给出了c#中的错误

时间:2014-03-19 18:28:13

标签: c# algorithm data-structures generic-programming

我是c#beginner并创建huffman tree,适用于" int"键入符号,但下一步是将其设为generic。在解释中,symbol应适用于每个data type每种数据类型我的意思是符号可以是int,ulong类型实际上我正在读binary file并试图找到每个符号重复的频率(符号重复的次数是它的频率),所以我的节点中的这个符号应该适用于& #34;短","长","未签名"在32/64位架构上。目前我使用了" int"在我的代码中,它可能是"简短"或任何其他。

我试过但我有错误,我的代码在下面给出,它包含有错误的行号。错误是: 并且获得的错误是:

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, "");

                }
            }
        }

有人可以帮助我解决这些错误并制作这个&#34;符号&#34;适用于所有数据类型,例如&#34;短&#34;,&#34; long&#34;等

1 个答案:

答案 0 :(得分:2)

这里至少有两个问题。

  • 您的计划中没有类型Node;对Node的引用需要改为Node<K>
  • 解决上一个项目后,您会发现您在多个地方都假设您将始终拥有Node<int>,但不一定是这种情况。您需要弄清楚如何将ReadByte()的输出放入K以及如何将其与pt.symbol进行比较。
  • 编译器不希望嵌套类定义中使用相同的占位符类型。而不是class Node<K>,请尝试class Node<T> where T : K
  • Main()的第一行不能使用占位符类型。它必须是树将存储的数据类型。