2014-11-14 3 views
-2

내가이상한 형식 불일치 오류

Type mismatch: cannot convert from MyLinkedList.Node<Type> to MyLinkedList.Node<Type> 

에서 모두 말한다 Eclipse에서 이상한 유형 불일치 오류가 점점 오전과 동일 할 수 있습니다. 스틸 유형 불일치? :(

코드 :이 캐스트 적용하는 경우

import java.util.Iterator; 


public class MyLinkedList<Type> implements Iterable<Type> { 

private Node<Type> head; 

// default contructor initializes the null list 
public MyLinkedList(){ 
    head = null; 
} 

// Static Inner Node Class 
private static class Node<Type>{ 
    private Type data; 
    private Node<Type> next; 

    public Node(Type dataValue, Node<Type> nextNode){ 
     this.data = dataValue; 
     this.next = nextNode; 
    } 
} 

@Override 
public Iterator<Type> iterator() { 
    // TODO Auto-generated method stub 
    return null; 
} 

private class LinkedListIterator<Type>{ 
    private Node<Type> nextNode; 
    public LinkedListIterator(){ 
///////////////////////////////// ERROR /////////////////////////// 
      nextNode = head; 
    } 
    } 
} 

오류가 도망 간다 :.

nextNode = (Node<Type>) head; 
+0

'유형'이라는 클래스가 있습니까? – pennstatephil

+3

실제 코드를 알려주십시오. Generics는 까다 롭기 때문에 코드 없이는 오류를 구분하기가 어렵습니다. –

+1

당신의'Type' 클래스 참조가 다릅니다. 한 패키지에는'Type'이 있고 다른 패키지에는'Type' 클래스가 있습니다. –

답변

4

비 정적 내부 클래스 LinkedListIterator 코드에서 새로운 제네릭 형식 Type를 정의를하지만 그것을 실제로 외부 클래스의 제네릭 형식을 상속합니다 MyLinkedList 반복기의 클래스 정의를

으로 변경하면됩니다.
private class LinkedListIterator{ 

그런 다음 작동해야합니다.

정적 내부 클래스 만 generic 형식을 상속하지 않으므로 Node의 제네릭 클래스 정의가 정확합니다.