C ++继承/未声明的标识符问题 [英] C++ inheritance/undeclared identifier issue

查看:141
本文介绍了C ++继承/未声明的标识符问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从同伴网站上为其中一本教科书下载了一些C ++文件.我将文件添加到Xcode,并编写了一个函数来测试所下载的类.

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

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

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.

推荐答案

root是从属名称,需要特别考虑才能将其放入范围或强制查找.您没有包括访问变量的代码,但是此代码非常糟糕,以至于我怀疑作者是否正确.

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.

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

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

实际上,我认为更正确地说,您想要的root是从属名称,除非您强迫它使用从属名称查找,否则不会.这意味着它会在模板实例化之前在依赖范围之外查找root,因此找不到您期望的root.如果他们根本不愿意编译示例代码,那么作者可能使用了不使用两阶段查找的编译器,例如MSVC ++.编译器应该进行两阶段查找.如果范围内某处有一个非依赖的root名称,则正确的编译器将使用它而不是基类的版本.

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.

这篇关于C ++继承/未声明的标识符问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆