root in a binary search tree

Hopefully, this might be an easy question.

I am working on a problem for a class I am taking. We just have to modify the driver some, so I have taken the existing code and copied into my editor/compiler (Bloosshed's DEV C++ 4.9.9.2), and am attempting to compile the exisitng code before making my changes. But I am getting the following error: "\binarySearchTree.h `root' undeclared (first use this function)"

From what I've read, isn't 'root' a special word that the compiler should know? Or is 'root' something that I need to declare somewhere?

Below is a code segment from "binarySearchTree", which is derived of "binaryTree". If additional code would be helpfull, let me know.

Thanks

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
template<class elemType>
bool bSearchTreeType<elemType>::search(const elemType& searchItem)
{
    nodeType<elemType> *current;
    bool found = false;

    if(root == NULL)
       cerr<<"Cannot search the empty tree."<<endl;
    else
    { 
       current = root;

       while(current != NULL && !found)
       {
             if(current->info == searchItem)
                found = true;
              else
                  if(current->info > searchItem)
                     current = current->llink;
                  else
                     current = current->rlink;
       }//end while
    }//end else

    return found;
}//end search 
root is not a keyword. You need to declare it somewhere.
(like as a data member of bSearchTreeType<>).
Thanks Jsmith! After seeing your reply, I see that *root is declared in the class binaryTree, but wasn't declared in class binarySearchTree. Once I declared it there, it compiled correctly.

Thanks for your help.
Here's another question then. When I was finally able to log into my school's computer, where we are using emacs as our editor/compiler, it compiled just fine, without defining *root in bSerchTree. *root is defined in the base class binaryTree, so I would think (though I'm clearly new to this) that I wouldn't need to define it in the derived class. That thinking bears out using the school's compiler, but not mine.

Should *root need to be declared in both the base and derived classes, or is my compiler just wonkey?

As a side note, after getting it to compile (having defined *root in the derived class) on my machine, the program didn't run anyway. After entering 1 number into the tree, I was getting a Windows error. Am I wrong to think that could be due to the multiple instences of *root?

It should not be declared in the derived class if it is already in the base class.

Is the base class a template class? (How is the base class declared?)


Topic archived. No new replies allowed.