C graphics
C graphics

Reputation: 7458

Java: cannot be resolved to a type

What is wrong with my code below where I have defined a nested class? It complains and says: CNode cannot be resolved to a type

package compute;

public class CBTree {

   public CNode root;

   public void addNode(int key){
      CNode newNode = new CNode(key);
      // now to find an appropriate place for this new node

// 1 it could be placed at root
      if (null == root){ // to void c-style mistakes a==null ( with a=null) is not prefered
         root = newNode;
      }
// if not then find the best spot ( which will be once of the
      CNode currentRoot = root;
      while(true){
         if (key < currentRoot.key){
            if (null == currentRoot.left){
               currentRoot.left = newNode;
               break;
            } else{
               currentRoot = currentRoot.left;
            } else{//if (key < currentRoot.key)
               if (null == currentRoot.right){
                  currentRoot.right = newNode;
                  break;
               }else{
                  currentRoot = currentRoot.right;
               }

            }

         }//while



         class CNode{
            int key;
            public CNode left;
            public CNode right;
            /**
             * Constructor
             */
            public CNode(int key){
               this.key = key;
            }
            /**
             * Display the node
             */
            public void display(){
               System.out.println("node:"+ key);
            }

         }


      }

Upvotes: 1

Views: 1389

Answers (4)

user2891207
user2891207

Reputation:

Putting a nested class inside a method means that any ability to refer to that class before or after the method is running will fail. Put your nested class inside your main class, but outside of any methods.

Upvotes: 1

rgettman
rgettman

Reputation: 178293

The CNode class is defined in the addNode method.

Place your CNode class outside of the addNode method so it can be resolved.

Additionally, you will need to adjust your if/else logic, because you currently have two else blocks on the same if, which won't compile.

Upvotes: 3

JTFRage
JTFRage

Reputation: 397

The following code needs to be placed outside the addNode method

class CNode{
                int key;
                public CNode left;
                public CNode right;
                /**
                 * Constructor
                 */
                public CNode(int key){
                   this.key = key;
                }
                /**
                 * Display the node
                 */
                public void display(){
                   System.out.println("node:"+ key);
                }

             }

Upvotes: 0

Robert Gannon
Robert Gannon

Reputation: 253

In addition to rgettman's suggestion, you could also make CNode a static class within CBTree and instantiate it with CBTree.CNode.

Also, your bracketing looks off. Your comment ending your while block seems to correspond to your if block.

This question is very similar to this.

Upvotes: 1

Related Questions