Results 1 to 8 of 8
  1. #1
    niu_niu is offline Member
    Join Date
    Jun 2010
    Posts
    48
    Rep Power
    0

    Default Got hit by the Comparable block again, help pls.

    Java Code:
    import java.util.*;
    
    public class aaBSTNode <T extends Comparable<T>> {
    				protected T el; 
    				protected aaBSTNode<T> left, right; 
    			
    				public aaBSTNode( ){
    				left=right=null;
    				}
    			
    				public aaBSTNode(T el) {
    				this (el, null, null); 
    				}
    				
    				public int compareTo(Object o) {
    			           	if (o instanceof aaBSTNode) {
    			                      aaBSTNode temp = (aaBSTNode) o;			              
    			                      	return ((temp.el) - (this.el));  
    			           	} else {
    			            throw new RuntimeException ("wrong class"); 
    			            }
    			     }
    			        
    				public aaBSTNode(T el, aaBSTNode<T> lt, aaBSTNode<T> rt) {
    				this.el =el; left =lt; right =rt; 
    				}
    			
    }
    ---------------

    Hi:

    I am getting an error on this line of code:

    return ((temp.el) - (this.el));

    The error message says: "The operator - is undefined for the argument type(s) Comparable, T". I have been trying to figure this out for days, but in vain, so some help is highly appreciated!

    Niu

  2. #2
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default

    Why are you trying to subtract two objects? Only primitive data can use arithmetic operations.

    EDIT: Some irrelevant criticism:

    1. All the constructors should be together.
    2. Use an IDE to write your source code (Eclipse, NetBeans, Notepad++, check out this list: What are you using to write your code? ) so the indentation isn't arbitrary.
    Last edited by Lil_Aziz1; 06-23-2010 at 06:39 PM.
    "Experience is what you get when you don't get what you want" (Dan Stanford)
    "Rise and rise again until lambs become lions" (Robin Hood)

  3. #3
    niu_niu is offline Member
    Join Date
    Jun 2010
    Posts
    48
    Rep Power
    0

    Default

    you answer doesn't really help me.

  4. #4
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    Quote Originally Posted by niu_niu View Post
    you answer doesn't really help me.
    Your question doesn't help us. He asked why you're trying to subtract two objects, because its not possible to do that... We don't really have enough information to help you out here...
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  5. #5
    niu_niu is offline Member
    Join Date
    Jun 2010
    Posts
    48
    Rep Power
    0

    Default

    I don't have to. I tried NULL but it doesn't work. What are my options here?

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,544
    Rep Power
    23

    Default

    return ((temp.el) - (this.el));
    What is this line of code supposed to do? Can you explain?

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,432
    Rep Power
    18

    Default

    Put it this way, your bit of code there is the equivalent of subtracting one Bicycle from another Bicycle.

    ETA: Stab in the dark here, but are you actually supposed to be comparing the two objects, temp.el and this.el? In which case you should be doing temp.el.compareTo(this.el). Though I am a bit confused about what this class is supposed to be for...but that's another story.
    Last edited by Tolls; 06-24-2010 at 09:38 AM.

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,995
    Blog Entries
    7
    Rep Power
    19

    Default

    Quote Originally Posted by Tolls View Post
    Put it this way, your bit of code there is the equivalent of subtracting one Bicycle from another Bicycle.
    I know that one! That's a nocycle! What did I win? Am I going to be famous now?

    kind regards,

    Jos (<--- celebrity ;-)

Similar Threads

  1. Comparable Interface
    By Yelrubk in forum New To Java
    Replies: 3
    Last Post: 04-28-2010, 02:46 PM
  2. Convert Comparable object to string or char
    By ScKaSx in forum New To Java
    Replies: 4
    Last Post: 01-25-2009, 02:02 PM
  3. Creating a Comparable object
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-15-2008, 07:38 PM
  4. interface Comparable<T> problem
    By Lennon-Guru in forum New To Java
    Replies: 3
    Last Post: 03-05-2008, 12:17 AM
  5. Using Comparable and Comparator interfaces
    By barney in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 07:10 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •