counting nodes in binary tree

I'm trying to count the number of node in a binary tree. I have a public count function that calls a private count function. I keep getting a linker error: Undefined symbols for architecture x86_64:
"BST<int>::nodesCount(BST_Node<int>*, int&)", referenced from:
BST<int>::nodesCount() in main.o
This is in Xcode. It doesn't seem to like calling the private function in the public one, but I'm not sure why since I'm doing that with several other functions.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//public function
template <typename T>
int BST<T>::nodesCount()
{
   int count = 0;
   nodesCount(mRootNode, count);
   return count;
}


//private function
template <typename T>
void nodesCount(BST_Node<T> *node, int &count)
{
   if(node == NULL)
      return;
   else
      count = 1 + nodesCount(node->mLeft, count) + nodesCount(node->mRight, count);
}


1
2
//how it's called in main
cout << "Node Count = " << tree.nodesCount() << endl;
Last edited on
Aren't you missing the class name and scope resolution operator on your private function?
i.e.:
void BST<T>::nodesCount(BST_Node<T> *node, int &count)
yup, that was the problem. Thanks.
Topic archived. No new replies allowed.