C#:有人知道如何解决这个问题吗? :类型或命名空间名称“T”找不到

发布于 2024-08-24 06:01:09 字数 3381 浏览 1 评论 0原文

我真的很难修复我的代码,想知道是否有人可以帮助我。

基本上我收到以下错误:

找不到类型或命名空间名称“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

如梦亦如幻 2024-08-31 06:01:09

您必须在此处指定具体类型,而不是类型占位符 T

    public static void Main(string[] args) 
    { 
        LinkListGen<T> testList = new LinkListGen<T>(); 
                    ^                             ^
        Console.ReadKey(); 
    } 

例如:

        LinkListGen<string> testList = new LinkListGen<string>(); 

You have to specify a concrete type here, rather than the type placeholder, T:

    public static void Main(string[] args) 
    { 
        LinkListGen<T> testList = new LinkListGen<T>(); 
                    ^                             ^
        Console.ReadKey(); 
    } 

For example:

        LinkListGen<string> testList = new LinkListGen<string>(); 
⒈起吃苦の倖褔 2024-08-31 06:01:09
LinkListGen<T> testList = new LinkListGen<T>();

您应该将此处的“T”替换为您尝试用于通用列表的类型。

LinkListGen<T> testList = new LinkListGen<T>();

You should replace 'T' here with the type you're trying to use for your generic list.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文