C++继承/未声明的标识符问题

C++ inheritance/undeclared identifier issue

本文关键字:标识符 问题 未声明 继承 C++      更新时间:2023-10-16

我从配套网站为我的一本教科书下载了一些C++文件。我将这些文件添加到Xcode中,并编写了一个函数来测试我下载的类。

这两个文件是二进制树的类,然后是从二进制树类继承的二进制搜索树的类。

我的问题是,当子类继承超级类时,超级类的一个成员没有被声明。我列举了我见过的所有例子,但我看不出有什么问题。我在下面发布了相关的代码以及我得到的错误。

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.

下面是我用来测试类的代码:

#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);
}

我得到的错误是,每当创建bSearchTreeType的对象时,都没有声明binaryTree超类的成员变量root

root是一个依赖名称,这需要特别考虑将其纳入范围或强制查找。您没有包含访问变量的代码,但这段代码太糟糕了,我怀疑作者是否做对了。

请让我们知道这是哪本书BTW,这样它就可以被列入要避免的书籍列表。仅命名惯例就让我想吐。然而,也存在一些明显的技术缺陷,例如缺乏受保护或虚拟析构函数的类型的公共继承。。。毫无理由。

要在bSearchTreeType<elemType>的范围内正确访问root,需要使用binaryTreeType<elemType>::root

编辑:事实上,我认为更正确地说,你想要的root是一个依赖名称,除非你强迫它使用依赖名称查找,否则它不会。这意味着它在模板实例化之前在依赖作用域之外查找root,因此找不到您期望的root。作者可能使用了一个不使用两阶段查找的编译器,如MSVC++。。。如果他们真的要编译示例代码的话。编译器应该进行两阶段查找。如果作用域中的某个位置存在非依赖的root名称,则正确的编译器将使用它而不是基类的版本。