我从配套网站下载了一些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);
}
我得到的错误是,只要创建了root
的对象,就不会声明binaryTree
超类的成员变量bSearchTreeType
。
答案 0 :(得分:1)
root
是一个从属名称,需要特别考虑将其纳入范围或强制查找。你没有包含访问变量的代码,但这段代码非常糟糕,我怀疑作者是否正确。
请告诉我们哪本书是BTW,以便将其列入要避免的书籍清单。单独的命名惯例让我想要呕吐。还有一些明显的技术缺陷,例如缺乏受保护或虚拟析构函数的类型的公共继承......并且无缘无故。
要在root
范围内正确访问bSearchTreeType<elemType>
,您需要使用binaryTreeType<elemType>::root
。
编辑:实际上我认为更准确地说,你想要的root
是一个从属名称,除非你强制它使用从属名称查找,否则它不会。这意味着它在模板实例化之前在依赖范围之外查找root
,因此找不到您期望它的root
。作者可能使用了一个不使用两阶段查找的编译器,如MSVC ++ ...如果他们根本不打算编译他们的示例代码。编译器应该进行两阶段查找。如果在范围内某处存在非依赖root
名称,则正确的编译器将使用它而不是基类的版本。