Reputation: 713
I am trying to create a small class in swift but getting the following error argument type T.Type
does not conform to expected type Comparable
Can someone help?
struct BST<T: Comparable> {
let root: Node<T>?
var count = 0
init<T:Comparable>(data: T) {
self.root = Node(data : T) //Error Occurs in this line
}
}//end class BST
Here is code of Node
class.
class Node<T: Comparable> {
let data: T
var left: Node?
var right: Node?
init(data: T) {
self.data = data
} //end init
} //end class node
func == <T> (lhs: Node<T>, rhs: Node<T>) -> Bool {
return lhs.data == rhs.data
} //end ==
func < <T> (lhs: Node<T>, rhs: Node<T>) -> Bool {
if(rhs.data > lhs.data) {
return true
}
return false
} //end fun <
Upvotes: 2
Views: 2552
Reputation: 7552
self.root = Node(data : T) //Error Occurs in this line
You are trying to initialize a Node
with the type, instead of the value.
Try
self.root = Node(data : data)
Upvotes: 2
Reputation: 2902
You need to pass data
in the initializer not T
. and also there is no need to make initialize generic.
Change your code:
struct BST<T: Comparable> {
let root: Node<T>?
var count = 0
init(data: T) {
self.root = Node(data: data)
}
}//end class BST
Upvotes: 5