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
const&.) What does your debugger tell you about the two values you're comparing when you run that code?B.setCount()you need to compare to? My gut feeling tells me you want to compare toB.count! Oh, and it doesn't matter ifcountis private.this->countand manually check if its a flaw or something true . May be the condition is always true and you have not implementedsetCountfunction correctly