具有通用实现的单链表

本文关键字:链表 单链表 实现 | 更新日期: 2023-09-27 17:59:45

我创建了一个具有通用实现的单个链表。但是Add(..)方法给出的编译错误是:

错误4无法隐式转换类型"ds"。MyNode<T>'to'ds.MyNode<T>'

遵循代码实现:

public class MyNode<T>
{
    public MyNode(T content)
    {
        Content = content;
    }
    public T Content { get; set; }
    public MyNode<T> Next { get; set; }
}
public class MyLinkedList<T>
{
    private int size;
    private MyNode<T> head;
    private MyNode<T> tail;
    public MyNode<T> Tail
    {
        get { return tail; }
        set { tail = value; }
    }
    public int Count
    {
        get { return size; }
        set { size = value; }
    }
    public MyNode<T> Head
    {
        get { return head; }
        set { head = value; }
    }
    public void Add<T>(MyNode<T> node)
    {
        size++;
        if (head == null)
        {
            head = tail = node;
        }
        else
        {               
            tail.Next = node;
            tail = node;
        }
    }       
}

我不确定我在这里遗漏了什么,这个错误令人困惑,因为它说的两个类型都是不可隐式转换的。感谢您的帮助。

我正在根据.Net 4.0 进行编译

谢谢。

具有通用实现的单链表

只需从Add方法中删除<T>泛型类型,因为您的类已经是泛型了。

类和方法可能具有相同的泛型类型(文档)名称:

如果定义了一个泛型方法,该方法采用与包含类相同的类型参数,则编译器会生成警告CS0693因为在方法范围内,为内部T提供的参数隐藏为外部T提供的参数。如果您需要用类型参数调用泛型类方法的灵活性除了在类被实例化时提供的那些之外,考虑为方法的类型参数提供另一个标识符,如以下示例中的GenericList2<T>所示。

class GenericList<T>
{
    // CS0693 
    void SampleMethod<T>() { }
}
class GenericList2<T>
{
    //No warning 
    void SampleMethod<U>() { }
}

因此,您可能应该启用编译警告。ideone.com的编译器输出示例:

prog.cs(39,22): warning CS0693: Type parameter `T' has the same name as the type parameter from outer type `Test.MyLinkedList<T>'
prog.cs(15,28): (Location of the symbol related to previous warning)
prog.cs(44,28): error CS0029: Cannot implicitly convert type `Test.MyNode<T> [prog, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null]' to `Test.MyNode<T> [prog, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null]'
prog.cs(48,26): error CS0029: Cannot implicitly convert type `Test.MyNode<T> [prog, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null]' to `Test.MyNode<T> [prog, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null]'
prog.cs(49,21): error CS0029: Cannot implicitly convert type `Test.MyNode<T> [prog, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null]' to `Test.MyNode<T> [prog, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null]'
Compilation failed: 3 error(s), 1 warnings

这:

public void Add<T>(MyNode<T> node)

意味着T过度遮蔽了类级T声明,因此编译器将它们视为不同的类型。删除T将起作用,因为很明显,您需要类T声明。

只是为了让你直观地看到它,这也会起作用(不要使用这个)

public void Add<TOther>(MyNode<T> node) where TOther : T

由于您现在明确地告诉编译器TOtherT类型或派生类型。