I am working on c# and am beginner. I am under a situation that I am creating Huffman tree where I calculate the frequency of the symbols in a binary file (I mean number of times the symbol repeats is the frequency).I tried to make this "symbol" work for all data types like int
, short
,ulong
etc. I do so using generics.
And I then I tried to run the code I am getting 4 errors like :
CS0246: The type or namespace name `T' could not be found. Are you missing a using directive or an assembly reference?
I know compiler is not able to recognize this "T
" but before "T
" I was using just public class Node<K>
instead of public class Node<T> where T : K
so that time the error was :
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)
So I was obliged to replace this "K
" with equivalent "T
". But now the error grown to be 4 (similar):
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?
My full code is:
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();
//////////////////////////////
}
}
}
Could some one please help me in removing these errors ? I would really appreciate. But please remember if I do "public class Node<K>
" instead of "public class Node<T> where T : K
" in Line 13. It gives these errors:
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)
Let's look at a massively reduced version of this:
public class Huffman<K>
{
public class Node<T> where T : K
{
}
public Node<T> front, rear;
}
What is the type of front
and rear
here? It refers to T
, but we have no idea what T
is. T
is a type parameter in Node<T>
, but when you have the declaration of a field you need to provide a type argument.
I suspect you actually want:
public Node<K> front, rear;
It be honest it's not clear that you need Node
to be a generic class at all. I suspect you could probably be fine with:
public class Huffman<K>
{
public class Node
{
// You can still use K here...
}
public Node front, rear;
}
Think about whether you really need to two different type parameters here - where's the benefit in doing so?