简体   繁体   中英

Compile error while calling generic function and assigning generic variable

I cant find what is wrong in my following code, I'm getting the following errors
Cannot implicitly convert type 'DelegatePractice.Node' to 'DelegatePractice.Node'

Cannot implicitly convert type 'DelegatePractice.Node' to 'DelegatePractice.Node'

class LinkedList<T>
{
    internal Node<T> node;
   internal void Insert<T>(T data)
    {

        Node<T> n = new Node<T>(data);
        if (node == null) node = n;//compile error
        else
        {
            Node<T> lastNode = getLastNode(this);//compile error
            lastNode.next = n; 
        }

    }

    internal Node<T> getLastNode(LinkedList<T> linkedList)
    {
        Node<T> tempNode = linkedList.node;
        while (tempNode.next != null)
        {
            tempNode = tempNode.next;
        }
        return tempNode;
    }
}
class Node<T>
{
   public T data;
   public Node<T> next;
    public Node(T d)
    {
        data = d;next = null;
    }   
}

You need to declare Insert method without introducing extra generic type, as:

internal void Insert(T data)
{
    ...

You don't need the generic qualifier on the Insert method, I don't think, since you're passing the type into the method.

internal void Insert(T data)

https://gist.github.com/f3/55283bb99e131eaa48eb348fd9647ee7

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM