C#:有人知道如何解决这个问题吗? :类型或命名空间名称“T”找不到
我真的很难修复我的代码,想知道是否有人可以帮助我。
基本上我收到以下错误:
找不到类型或命名空间名称“T”(您是否缺少 using 指令或程序集引用?)
以下是我的类:
程序类:
using System;
using System.Collections.Generic;
using System.Text;
namespace LinkedListGen
{
class program
{
public static void Main(string[] args)
{
LinkListGen<T> testList = new LinkListGen<T>();
Console.ReadKey();
}
}
}
LinkGen 类:
using System;
using System.Collections.Generic;
using System.Text;
namespace LinkedListGen
{
class LinkGen<T>
{
private T data;
private LinkGen<T> next;
public LinkGen(T item)
{
data = item;
next = null;
}
public LinkGen(T item, LinkGen<T> list)
{
data = item;
next = list;
}
public LinkGen<T> TailList
{
set { this.next = value; }
get { return this.next; }
}
public T HeadList
{
set { this.data = value; }
get { return this.data; }
}
}
}
LinkListGen 类:
using System;
using System.Collections.Generic;
using System.Text;
namespace LinkedListGen
{
public class LinkListGen<T> where T : IComparable
{
private LinkGen<T> list;
public LinkListGen() //initialise list to be empty
{
list = null;
}
public void AddItem(T item)
{
list = new LinkGen<T>(item, list);
}
public string DisplayList() //write items to string
{
LinkGen<T> temp = list;
string buffer = "";
while (temp != null)
{
Console.WriteLine(temp.HeadList);
temp = temp.TailList;
}
return buffer;
}
public int NumberOfItems()
{
LinkGen<T> temp = list;
int count;
count = 0;
while (temp != null)
{
count++;
temp = temp.TailList;
}
Console.Out.WriteLine("There are " + count + "items recorded.");
return count;
}
public bool IsPresentItem(T item)
{
bool txf;
LinkGen<T> temp = list;
int count;
count = 0;
while (temp != null)
{
if (item.Equals(temp.HeadList))
{
count++;
}
temp = temp.TailList;
}
if (count > 0)
{
Console.Out.WriteLine("There are " + count + " instances of " + item + ".");
txf = true;
}
else
{
Console.Out.WriteLine("There are no instances of " + item + ".");
txf = false;
}
return txf;
}
public void RemoveItem(T item)
{
LinkGen<T> prev = list;
LinkGen<T> curr = list;
if (item.Equals(curr.HeadList))
list = curr.TailList;
else
{
while (curr != null)
{
if (item.Equals(curr.HeadList))
{
prev.TailList = curr.TailList;
}
else
{
prev = curr;
curr = curr.TailList;
}
}
}
}
}
}
目标是创建一个通用链接列表
我真的束手无策,非常感谢提供的任何帮助。
Im really having trouble fixing my code, was wondering if there was anyone out there who could help me.
Basically im getting the following error:
The type or namespace name 'T' could not be found (are you missing a using directive or an assembly reference?)
The following are my classes:
Program Class:
using System;
using System.Collections.Generic;
using System.Text;
namespace LinkedListGen
{
class program
{
public static void Main(string[] args)
{
LinkListGen<T> testList = new LinkListGen<T>();
Console.ReadKey();
}
}
}
LinkGen Class:
using System;
using System.Collections.Generic;
using System.Text;
namespace LinkedListGen
{
class LinkGen<T>
{
private T data;
private LinkGen<T> next;
public LinkGen(T item)
{
data = item;
next = null;
}
public LinkGen(T item, LinkGen<T> list)
{
data = item;
next = list;
}
public LinkGen<T> TailList
{
set { this.next = value; }
get { return this.next; }
}
public T HeadList
{
set { this.data = value; }
get { return this.data; }
}
}
}
LinkListGen Class:
using System;
using System.Collections.Generic;
using System.Text;
namespace LinkedListGen
{
public class LinkListGen<T> where T : IComparable
{
private LinkGen<T> list;
public LinkListGen() //initialise list to be empty
{
list = null;
}
public void AddItem(T item)
{
list = new LinkGen<T>(item, list);
}
public string DisplayList() //write items to string
{
LinkGen<T> temp = list;
string buffer = "";
while (temp != null)
{
Console.WriteLine(temp.HeadList);
temp = temp.TailList;
}
return buffer;
}
public int NumberOfItems()
{
LinkGen<T> temp = list;
int count;
count = 0;
while (temp != null)
{
count++;
temp = temp.TailList;
}
Console.Out.WriteLine("There are " + count + "items recorded.");
return count;
}
public bool IsPresentItem(T item)
{
bool txf;
LinkGen<T> temp = list;
int count;
count = 0;
while (temp != null)
{
if (item.Equals(temp.HeadList))
{
count++;
}
temp = temp.TailList;
}
if (count > 0)
{
Console.Out.WriteLine("There are " + count + " instances of " + item + ".");
txf = true;
}
else
{
Console.Out.WriteLine("There are no instances of " + item + ".");
txf = false;
}
return txf;
}
public void RemoveItem(T item)
{
LinkGen<T> prev = list;
LinkGen<T> curr = list;
if (item.Equals(curr.HeadList))
list = curr.TailList;
else
{
while (curr != null)
{
if (item.Equals(curr.HeadList))
{
prev.TailList = curr.TailList;
}
else
{
prev = curr;
curr = curr.TailList;
}
}
}
}
}
}
The aim is to create a generic linked list
Im really at my wits end and would appreciate any help offered.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您必须在此处指定具体类型,而不是类型占位符 T:
例如:
You have to specify a concrete type here, rather than the type placeholder, T:
For example:
您应该将此处的“T”替换为您尝试用于通用列表的类型。
You should replace 'T' here with the type you're trying to use for your generic list.