gpt4 book ai didi

c# - CS0246 : The type or namespace name `T' could not be found. 是否缺少 using 指令或程序集引用?在 C# 中

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:14:00 24 4
gpt4 key购买 nike

我正在使用 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)

所以我不得不用等效的“K”替换这个“T”。但是现在错误增长到 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是。 TNode<T> 中的类型参数 , 但是当你有一个字段的声明时,你需要提供一个类型参数

我怀疑你真的想要:

public Node<K> front, rear;

老实说,不清楚您是否需要 Node成为一个通用类。我怀疑您可能会觉得:

public class Huffman<K>
{
public class Node
{
// You can still use K here...
}

public Node front, rear;
}

想想你是否真的需要两个不同类型的参数 - 这样做有什么好处?

关于c# - CS0246 : The type or namespace name `T' could not be found. 是否缺少 using 指令或程序集引用?在 C# 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22529549/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com