2014-10-26 37 views
1

我從我的一本教科書的同伴網站下載了一些C++文件。我將這些文件添加到Xcode中,然後我編寫了一個函數來測試我下載的類。C++繼承/未聲明的標識符問題

這兩個文件是二叉樹的類,然後是從二叉樹類繼承的二叉搜索樹的類。

我的問題是,超類的成員之一是沒有得到聲明時,子類繼承超類。我遵循我見過的所有例子,但沒有看到問題。我發佈了下面的相關代碼以及我得到的錯誤。

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

T我得到的錯誤是binaryTree超類的成員變量root沒有在創建對象bSearchTreeType時被聲明。

+0

我們缺少binarySearchTree.cpp,這是問題所在。 – Zenexer 2014-10-26 20:43:25

+1

我當然希望我們期望在這裏看到的代碼不在cpp文件中! – 2014-10-26 20:44:56

+0

不,所有的定義都在.h文件中,但我只包含類定義,因爲我不認爲方法定義是相關的。 – 2014-10-26 20:47:48

回答

1

root是一個從屬名稱,這需要特殊考慮才能將其納入範圍或強制查找。你沒有包含訪問變量的代碼,但是這個代碼非常糟糕,我懷疑作者是否正確。

請讓我們知道這是BTW的書,所以它可以放在書籍清單,以避免。命名約定本身讓我想嘔吐。雖然還有一些明顯的技術缺陷,例如缺乏保護類型或虛擬析構類型的公共繼承...無緣無故。

要在bSearchTreeType<elemType>範圍內正確訪問root,您需要使用binaryTreeType<elemType>::root

編輯:其實我認爲更正確地說,root你想要的是一個依賴名稱,除非你強迫它使用依賴名稱查找它不會。這意味着它在模板實例化之前查找依賴範圍之外的root,因此未找到您期望的root。作者可能使用了一個不使用兩階段查找的編譯器,如MSVC++ ......如果他們根本就不想編譯他們的示例代碼。編譯器應該執行兩階段查找。如果在範圍的某個地方存在非依賴的root名稱,則正確的編譯器將使用它而不是基類的版本。

+0

這本書是D.S. Malik使用C++的數據結構。 在'binarySearchTree.h'中使用變量的代碼正常使用它,就像任何其他變量一樣。我該如何解決?只需將'root'重命名爲其他內容? – 2014-10-26 20:49:08

+0

@chasemccoy - 編輯提供修復。 – 2014-10-26 20:52:07

+1

其中一條評論是「水上樂趣更有趣」。絕對是我喜歡在我想購買的書中看到的東西。 – Barry 2014-10-26 20:52:57