【问题标题】:C# : Anyone know how to fix this? : The type or namespace name 'T' could not be foundC#:有人知道如何解决这个问题吗? : 找不到类型或命名空间名称“T”
【发布时间】:2010-03-06 02:51:31
【问题描述】:

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

基本上我得到以下错误:

找不到类型或命名空间名称“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;
                }
            }
        }
    }
}
}

目的是创建一个通用的链表

我真的束手无策,如果能提供任何帮助,我将不胜感激。

【问题讨论】:

    标签: c# linked-list generic-list


    【解决方案1】:

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

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

    例如:

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

    【讨论】:

    • 嗯,没错,那只是一个占位符。当我第一次尝试用一种类型替换它时,我以为我做错了,我很傻。非常感谢。
    【解决方案2】:
    LinkListGen<T> testList = new LinkListGen<T>();
    

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

    【讨论】:

      猜你喜欢
      • 2013-11-17
      • 2013-03-25
      • 2011-06-28
      • 2023-01-23
      • 1970-01-01
      • 2021-08-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多