Results 1 to 8 of 8
- 06-23-2010, 06:28 PM #1
Member
- Join Date
- Jun 2010
- Posts
- 48
- Rep Power
- 0
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
- 06-23-2010, 06:34 PM #2
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)
- 06-23-2010, 06:51 PM #3
Member
- Join Date
- Jun 2010
- Posts
- 48
- Rep Power
- 0
you answer doesn't really help me.
- 06-23-2010, 06:54 PM #4
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
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!
- 06-23-2010, 07:17 PM #5
Member
- Join Date
- Jun 2010
- Posts
- 48
- Rep Power
- 0
I don't have to. I tried NULL but it doesn't work. What are my options here?
- 06-23-2010, 10:37 PM #6
What is this line of code supposed to do? Can you explain?return ((temp.el) - (this.el));
- 06-24-2010, 09:35 AM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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.
- 06-24-2010, 09:49 AM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,422
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
Comparable Interface
By Yelrubk in forum New To JavaReplies: 3Last Post: 04-28-2010, 02:46 PM -
Convert Comparable object to string or char
By ScKaSx in forum New To JavaReplies: 4Last Post: 01-25-2009, 02:02 PM -
Creating a Comparable object
By Java Tip in forum java.langReplies: 0Last Post: 04-15-2008, 07:38 PM -
interface Comparable<T> problem
By Lennon-Guru in forum New To JavaReplies: 3Last Post: 03-05-2008, 12:17 AM -
Using Comparable and Comparator interfaces
By barney in forum New To JavaReplies: 1Last Post: 08-07-2007, 07:10 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks