generic class

T

Tony Johansson

Hello!

I have made a generic class called Tree<T> to implement a BinaryTree below.
First I used type int to add a node to the binary Tree<T> in this way
Tree<int> tree1 = new Tree<int>(10);
and used Insert to add another node to the BinaryTree in this way
tree1.Insert(5);
This worked successfully to use int.

Now to my question. I now try to use Circle instead of an int and this
doesn't work. I have implemented the
IComparable<T> CompareTo in class Circle.
When I try to compile I get this compile error.
"Error 1 The type 'BinaryTree_Test.Circle' must be convertible to
'System.IComparable<BinaryTree_Test.Circle>' in order to use it as parameter
'T' in the generic type or method 'BinaryTree.Tree<T>'
F:\C#\BinaryTree\BinaryTree Test\Program.cs 13 13 BinaryTree Test"
I can't understand what the problem is?

I have all the classes listed below.
class Program
{
static void Main(string[] args)
{
Circle myCircle = new Circle(3);
Tree<Circle> tree1 = new Tree<Circle>(myCircle);
//Tree<int> tree1 = new Tree<int>(10);
// tree1.Insert(5);
}
}

public class Tree<T> where T : IComparable<T>
{
private T data;
private Tree<T> left;
private Tree<T> right;

public Tree(T NodeValue)
{
data = NodeValue;
left = null;
right = null;
}

public T NodeData
{
get { return data; }
set { data = value; }
}

public Tree<T> LeftTree
{
get { return left; }
set { left = value; }
}

public Tree<T> RightTree
{
get { return right; }
set { right = value; }
}

public void Insert(T newItem)
{
T currentNodeValue = NodeData;
if (currentNodeValue.CompareTo(newItem) > 0)
{
if (LeftTree == null)
LeftTree = new Tree<T>(newItem);
else
LeftTree.Insert(newItem);
}
else
{
if (RightTree == null)
RightTree = new Tree<T>(newItem);
else
RightTree.Insert(newItem);
}
}
}


public class Circle
{
private int radius;
public Circle(int radius)
{ this.radius = radius; }

public double Area()
{ return radius * radius * 3.14156; }

public int CompareTo(Circle other)
{
if (Area() == other.Area()) return 0;
if (Area() > other.Area()) return 1;
return -1;
}
}

//Tony
 
J

Jon Skeet [C# MVP]

Tony Johansson said:
Hello!

I have made a generic class called Tree<T> to implement a BinaryTree below.
First I used type int to add a node to the binary Tree<T> in this way
Tree<int> tree1 = new Tree<int>(10);
and used Insert to add another node to the BinaryTree in this way
tree1.Insert(5);
This worked successfully to use int.

Now to my question. I now try to use Circle instead of an int and this
doesn't work. I have implemented the
IComparable<T> CompareTo in class Circle.
When I try to compile I get this compile error.
"Error 1 The type 'BinaryTree_Test.Circle' must be convertible to
'System.IComparable<BinaryTree_Test.Circle>' in order to use it as parameter
'T' in the generic type or method 'BinaryTree.Tree<T>'
F:\C#\BinaryTree\BinaryTree Test\Program.cs 13 13 BinaryTree Test"
I can't understand what the problem is?

You've constrained T with a derivation constraint such that you can
only use a type argument (e.g. int or Circle) which implements
IComparable<itself>.

That's fine for int, as it implements IComparable<int>. It doesn't work
for Circle though, because that doesn't implement IComparable<Circle>.

You've got the CompareTo method, but you haven't actually said you're
implementing the interface.
 
P

Peter Duniho

[...]
Now to my question. I now try to use Circle instead of an int and this
doesn't work. I have implemented the
IComparable<T> CompareTo in class Circle.
When I try to compile I get this compile error.
"Error 1 The type 'BinaryTree_Test.Circle' must be convertible to
'System.IComparable<BinaryTree_Test.Circle>' in order to use it as
parameter
'T' in the generic type or method 'BinaryTree.Tree<T>'
F:\C#\BinaryTree\BinaryTree Test\Program.cs 13 13 BinaryTree Test"
I can't understand what the problem is?

The problem is exactly what the error is telling you: the Circle class
isn't convertible to IComparable<Circle>.

The int type works, because int implements IComparable<int>. You need to
implement IComparable<Circle> yourself if you expect to do anything that
requires comparing two circles.

By the way, .NET has a binary tree data structure already. It has the
unfortunate (for discoverability, that is) name of SortedList<TKey,
TValue>. But it is really a binary tree.

Pete
 
R

Rene

So what Jon and Peter are saying is to change your Circle class to something
like:


public class Circle : IComparable<Circle>
{
....
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

using iterator in a binaryTree 3
generic class 3
Implement Interface 8
Class factory desing question 10
IEnumerator/IEnumurable 6
b-tree 11
How to make my methods dynamic? 11
Null and Generic Type 2

Top