C++ inheritance/undeclared identifier issue

2019-05-31 04:42发布

I downloaded some C++ files from the companion site for one of my textbooks. I added the files to Xcode, and I wrote a function to test the classes that I downloaded.

The two files are classes for a Binary Tree and then a class for a Binary Search Tree that inherits from the Binary Tree class.

My problem is that one of the members of the super class isn't getting declared when the subclass inherits the super class. I am following all examples I've seen and I don't see the issue. I am posting the relevant code below as well as the error I am getting.

binaryTree.h:

#include <iostream>

using namespace std;

     //Definition of the node
template <class elemType>
struct binaryTreeNode
{
    elemType info;
    binaryTreeNode<elemType> *llink;
    binaryTreeNode<elemType> *rlink;
};

   //Definition of the class
template <class elemType>
class binaryTreeType
{
public:
    const binaryTreeType<elemType>& operator=
                 (const binaryTreeType<elemType>&); 
      //Overload the assignment operator.
    bool isEmpty() const;
      //Returns true if the binary tree is empty;
      //otherwise, returns false.
    void inorderTraversal() const;
      //Function to do an inorder traversal of the binary tree.
    void preorderTraversal() const;
      //Function to do a preorder traversal of the binary tree.
    void postorderTraversal() const;
      //Function to do a postorder traversal of the binary tree.

    int treeHeight() const;
      //Returns the height of the binary tree.
    int treeNodeCount() const;
      //Returns the number of nodes in the binary tree.
    int treeLeavesCount() const;
      //Returns the number of leaves in the binary tree.
    void destroyTree();
      //Deallocates the memory space occupied by the binary tree.
      //Postcondition: root = NULL;

    binaryTreeType(const binaryTreeType<elemType>& otherTree); 
      //copy constructor

    binaryTreeType();
      //default constructor

    ~binaryTreeType();   
      //destructor

protected:
    binaryTreeNode<elemType> *root;

private:
    void copyTree(binaryTreeNode<elemType>* &copiedTreeRoot,
                  binaryTreeNode<elemType>* otherTreeRoot);
      //Makes a copy of the binary tree to which 
      //otherTreeRoot points. The pointer copiedTreeRoot  
      //points to the root of the copied binary tree.

    void destroy(binaryTreeNode<elemType>* &p);
      //Function to destroy the binary tree to which p points. 
      //Postcondition: p = NULL

    void inorder(binaryTreeNode<elemType> *p) const;
      //Function to do an inorder traversal of the binary
      //tree to which p points.  
    void preorder(binaryTreeNode<elemType> *p) const;
      //Function to do a preorder traversal of the binary
      //tree to which p points.  
    void postorder(binaryTreeNode<elemType> *p) const;
      //Function to do a postorder traversal of the binary
      //tree to which p points.  

    int height(binaryTreeNode<elemType> *p) const;
      //Function to return the height of the binary tree
      //to which p points. 
    int max(int x, int y) const;
      //Returns the larger of x and y.
    int nodeCount(binaryTreeNode<elemType> *p) const;
      //Function to return the number of nodes in the binary 
      //tree to which p points 
    int leavesCount(binaryTreeNode<elemType> *p) const;
      //Function to return the number of leaves in the binary 
      //tree to which p points 
};

binarySearchTree.h:

#include "binaryTree.h"
#include <iostream>
#include <cassert>

using namespace std;

template <class elemType>
class bSearchTreeType: public binaryTreeType<elemType>
{
public:
    bool search(const elemType& searchItem) const;
      //Function to determine if searchItem is in the binary 
      //search tree.
      //Postcondition: Returns true if searchItem is found in the 
      //    binary search tree; otherwise, returns false.

    void insert(const elemType& insertItem);
      //Function to insert insertItem in the binary search tree.
      //Postcondition: If no node in the binary search tree has the
      //    same info as insertItem, a node with the info insertItem
      //    is created and inserted in the binary search tree.

    void deleteNode(const elemType& deleteItem);
      //Function to delete deleteItem from the binary search tree 
      //Postcondition: If a node with the same info as deleteItem 
      //    is found, it is deleted from the binary search tree.

private:
    void deleteFromTree(binaryTreeNode<elemType>* &p);
      //Function to delete the node to which p points is deleted
      //from the binary search tree.
      //Postcondition: The node to which p points is deleted from
      //    the binary search tree.

And the following is the code that I am using to test the classes:

#include <iostream>
#include "binarySearchTree.h"
using namespace std;
void testBinarySearchTree();
int main()
{
  testBinarySearchTree();

  return 0;
}
void testBinarySearchTree()
{
  bSearchTreeType<int> myTree;

  myTree.insert(50);
  myTree.insert(40);
  myTree.insert(30);
  myTree.insert(60);
  myTree.insert(70);
  myTree.insert(45);
  myTree.insert(65);
  myTree.insert(55);
}

The error that I am getting is that the member variable root of the binaryTree superclass is not being declared whenever an object of bSearchTreeType is created.

1条回答
Ridiculous、
2楼-- · 2019-05-31 05:25

root is a dependent name and this requires special consideration to either bring it into scope or force lookup. You didn't include the code that accesses the variable, but this code is terrible enough that I have doubts the author got it right.

Please let us know which book this is BTW so it can be put on the list of books to avoid. The naming conventions alone make me want to puke. There are also some glaring technical flaws though such as public inheritance of a type lacking protected or virtual destructor...and for no reason.

To access root correctly within the scope of bSearchTreeType<elemType> you need to use binaryTreeType<elemType>::root.

Edit: Actually I think that more correctly stated, the root you want is a dependent name and unless you force it to use dependent name lookup it won't. This means that it looks for root outside of dependent scopes before template instantiation and so doesn't find the root you're expecting it to. The author probably used a compiler that doesn't use two-stage lookup such as MSVC++...if they bothered to compile their example code at all. Compilers are supposed to do two-stage lookup. If there was a non-dependent root name somewhere in scope a correct compiler would use it instead of the base class's version.

查看更多
登录 后发表回答