Trying to overload the < and > operators in a c++ class
template <typename T>
bool BinaryTree<T>::operator < (BinaryTree<T> &B) {
return (this->count < B.setCount(0));
}
template <typename T>
float BinaryTree<T>::setCount(float c)
{
count += c;
return count;
}
Where setCount(0) returns the count of the B obj. But this always outputs true no matter the numbers compared.
Changes my code to
template <typename T>
bool BinaryTree<T>::operator < (const BinaryTree<T> &B) {
return (this->count < B.count);
}
printf("%c %lf\n", tree[0]->getData(), tree[0]->setCount(0));
printf("%c %lf\n", tree[1]->getData(), tree[1]->setCount(0));
Output >
a 0.750000
b 0.250000
if(tree[0] < tree[1])
printf("0 < 1\n");
else printf("1 > 0\n");
Output >
0 < 1
This:
suggests to me that
treeholds pointers to yourBinaryTree<T>.Here, you are comparing pointers, i.e memory addresses, not values:
You probably need