Sign in to follow this  
The Orange Peanut

[java] Why does this not compile?

Recommended Posts

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?

Share this post


Link to post
Share on other sites
Sneftel    1788
Impossible to tell, with only that much code. Post the definition of ThreadedTreeNode, as well as all other classes involved in the type determination.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Aldacron    4544
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this