[java] Why does this not compile?

Started by
3 comments, last by The Orange Peanut 16 years, 6 months ago
I have this section of code:

  public E findMin()
  {
      ThreadedTreeNode cursor = root;
				
		while(cursor.leftChild != null)
		{
		    cursor = cursor.leftChild;
	        }
		
		return cursor.data;
  }

and I get this error. ThreadedTree.java:24: incompatible types found : java.lang.Comparable required: E return cursor.data; If I return, for example, root.data (both root and cursor are the same type of nodes) it will compile. There is something obvious I'm sure I'm missing, but I'm not sure what it is. Any ideas?
Dat is off da hizzle fo shizzle dizzle
Advertisement
Impossible to tell, with only that much code. Post the definition of ThreadedTreeNode, as well as all other classes involved in the type determination.
The ThreadedTreeNode class is pretty bare bones.

class ThreadedTreeNode<E extends Comparable<E>>{   E data;                          // The data in the node.   ThreadedTreeNode<E> leftChild;   // Left child.   ThreadedTreeNode<E> rightChild;  // Right child}


Those are the fields. The only other function is the constructor.
Dat is off da hizzle fo shizzle dizzle
The cursor variable in your method needs to be declared as type ThreadedTreeNode<E>, rather than just ThreadedTreeNode:

public E findMin(){        ThreadedTreeNode<E> cursor = root;			while(cursor.leftChild != null)	{	    cursor = cursor.leftChild;	}		return cursor.data;}


The root member should be declared the same.
Ah, thanks, thats it. Root is declared that way, I just made a mistake on cursor. Thanks for pointing that out.
Dat is off da hizzle fo shizzle dizzle

This topic is closed to new replies.

Advertisement