简体   繁体   中英

CS0246: The type or namespace name `T' could not be found. Are you missing a using directive or an assembly reference? in c#

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?

In addition to the answer from Jon Skeet, when using Generic types in your methods, you need to add the generic declaration to your methods:

For example:

public void Print_tree(Node<T> treee)

Becomes:

public void Print_tree<T>(Node<T> treee)

And

public Node<T> find_two_smallest(ref  Node<T> pmin1, ref  Node<T> pmin2)

Becomes:

public Node<T> find_two_smallest<T>(ref  Node<T> pmin1, ref  Node<T> pmin2)

Even if you don't use it in the parameters but in the body of the method, the method declaration needs to know about it, your method huffman_node_processing() uses Node<T> in it's body. Thus Visual studio tells you that it needs to know it in the method declaration. By changing the declaration to

huffman_node_processing<T>()
you tell C# that this method uses type T .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

Related Question C# error CS0246 The type or namespace name 'Socket' could not be found (are you missing a using directive or an assembly reference) CS0246 C# The type or namespace name 'ForeignKeyAttribute' could not be found (are you missing a using directive or an assembly reference?) C# Error: Error CS0246 The type or namespace name '' could not be found (are you missing a using directive or an assembly reference?) error CS0246: The type or namespace name `AForge' could not be found. Are you missing a using directive or an assembly reference? error CS0246: The type or namespace name 'Npgsql' could not be found (are you missing a using directive or an assembly reference?) CS0246: The type or namespace name 'MySql' could not be found (are you missing a using directive or an assembly reference Error CS0246 The type or namespace name 'Windows' could not be found (are you missing a using directive or an assembly reference?) Error CS0246: The type or namespace name 'StreamingContext' could not be found (are you missing a using directive or an assembly reference?) error CS0246: The type or namespace name 'IWebHostEnvironment' could not be found (are you missing a using directive or an assembly reference?) CS0246: The type or namespace name 'Employee' could not be found (are you missing a using directive or an assembly reference?)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM