C ++从派生类访问基类中的结构

时间:2011-10-13 23:24:11

标签: c++ inheritance struct

修改为包含实际代码

我有一个班级

    // BinarySearchTree class provide by Mark Allen Weiss in Data Structures
    // and Algorithm Analysis in C++, 3ed
    //
    // Implementation is combined with specification.  No separate header file.

    #ifndef BINARY_SEARCH_TREE_H
    #define BINARY_SEARCH_TREE_H


    #include <iostream>
    using namespace std;

    template <typename Comparable>
    class BinarySearchTree {
    public:
    //Constructors
    BinarySearchTree( ) :root( 0 ) { }
    BinarySearchTree( const BinarySearchTree & rhs ) : root( 0 )
    {
        *this = rhs;
    }

    //Destructor
    ~BinarySearchTree( )
    {
        makeEmpty( );
    }


    /**
     * Find the smallest item in the tree.
     * Throw UnderflowException if empty.
     */
    const Comparable & findMin( ) const
    {
        return findMin( root )->element;
    }

    /**
     * Find the largest item in the tree.
     * Throw UnderflowException if empty.
     */
    const Comparable & findMax( ) const
    {
        return findMax( root )->element;
    }


    /**
     * Test if the tree is logically empty.
     * Return true if empty, false otherwise.
     */
    bool isEmpty( ) const
    {
        return root == 0;
    }



    /**
     * Print the tree contents in sorted order.
     */
    void printTree( ostream & out = cout ) const
    {
        if( isEmpty( ) )
            out << "Empty tree" << endl;
        else
            printTree( root ,out );
    }




    /**
     * Insert x into the tree; duplicates are ignored.
     */
    void insert( const Comparable & x )
    {
        insert( x, root );
    }

    /**
     * Remove x from the tree. Nothing is done if x is not found.
     */
    void remove( const Comparable & x )
    {
        remove( x, root );
    }

    /**
     * Deep copy.
     */
    const BinarySearchTree & operator=( const BinarySearchTree & rhs )
    {
        if( this != &rhs )
        {
            makeEmpty( );
            root = clone( rhs.root );
        }
        return *this;
    }


    //protected:
    friend struct BinaryNode
    {
        Comparable element;
        BinaryNode *left;
        BinaryNode *right;

        BinaryNode( const Comparable & theElement, BinaryNode *lt, BinaryNode* rt ) :
              element(theElement), left(lt), right(rt)
        { }

    };

    BinaryNode *root;

    private:
    /**
     * Internal method to insert into a subtree.
     * x is the item to insert.
     * t is the node that roots the subtree.
     * Set the new root of the subtree.
     */
    void insert( const Comparable & x, BinaryNode * & t )
    {
        if( t == 0 )
            t = new BinaryNode( x, 0, 0 );
        else if( x < t->element )
            insert( x, t->left );
        else if( t->element < x )
            insert( x, t->right );
        else
            ;  // Duplicate; do nothing
    }

    /**
     * Internal method to remove from a subtree.
     * x is the item to remove.
     * t is the node that roots the subtree.
     * Set the new root of the subtree.
     */
    void remove( const Comparable & x, BinaryNode * & t )
    {
        if( t == 0 )
            return;   // Item not found; do nothing
        if( x < t->element )
            remove( x, t->left );
        else if( t->element < x )
            remove( x, t->right );
        else if( t->left != 0 && t->right != 0 ) // Two children
        {
            t->element = findMin( t->right )->element;
            remove( t->element, t->right );
        }
        else
        {
            BinaryNode *oldNode = t;
            t = ( t->left != 0 ) ? t->left : t->right;
            delete oldNode;
        }
    }

    /**
     * Internal method to find the smallest item in a subtree t.
     * Return node containing the smallest item.
     */
    BinaryNode * findMin( BinaryNode *t ) const
    {
        if( t == 0 )
            return 0;
        if( t->left == 0 )
            return t;
        return findMin( t->left );
    }

    /**
     * Internal method to find the largest item in a subtree t.
     * Return node containing the largest item.
     */
    BinaryNode * findMax( BinaryNode *t ) const
    {
        if( t != 0 )
            while( t->right != 0 )
                t = t->right;
        return t;
    }

    /**
     * Internal method to print a subtree rooted at t in sorted order.
     */
    void printTree( BinaryNode *t, ostream & out ) const
    {
        if( t != 0 )
        {
            printTree( t->left, out );
            out << t->element << endl;
            printTree( t->right, out );
        }
    }


    /**
     * Internal method to clone subtree.
     */
    BinaryNode * clone( BinaryNode *t ) const
    {
        if( t == 0 )
            return 0;
        else
            return new BinaryNode( t->element, clone( t->left ), clone( t->right ) );
    }

    };

    #endif

包含结构。我有一个继承自上述类

的第二类“MyBST”
#include"BinarySearchTree.h"
using namespace std;

template <typename Comparable>
class MyBST : public BinarySearchTree<Comparable>
{
public:
    MyBST()
    {
        leftReplace = true;
    }

    void strictRemoval()
    {
        if (leftReplace)
        {
            removeLargestFromtLeft(root->element, root);
            leftReplace = false;
        }
        else
        {
            remove(root->element);
            leftReplace = true;
        }
        printTree();
    }


    bool leftReplace;
    void removeLargestFromLeft( const Comparable & x, BinaryNode * &t )
    {
        if( t == 0 )
            return;   // Item not found; do nothing
        if( x < t->element )
            removeLargestFromLeft(x, root->left);
        else if( t->element < x )
            removeLargestFromLeft(x, root->right);
        else if( t->left != 0 && t->right != 0 ) // Two children
        {
            t->element = findMax( t->left )->element;
            removeLargestFromLeft(t->element, root->left);
        }
        else
        {
            BinaryNode *oldNode = t;
            t = ( t->left != 0 ) ? t->left : t->right;
            delete oldNode;
        }
    }

    BinaryNode * findMax(BinaryNode * t)
    {
        // not implemented yet stopped here because the rest of the code was not 
        // working
    }
};

New With Edit

这是我的主要功能,将使用这两个类我只是试图让我的代码工作并测试它。

#include "BinarySearchTree.h"
#include "MyBST.h"

int main()
{
    MyBST<int> BST;
    BST.insert(3);
    BST.insert(4);
    BST.insert(5);
    BST.insert(6);
    BST.insert(6);
    BST.insert(7);
    BST.insert(8);
    BST.insert(9);
    BST.insert(1);
    BST.printTree();
    while (!BST.isEmpty())
        BST.strictRemoval();
    return 0;
};

当我编译这两个类时,我得到的错误是: - 'BinaryNode'尚未声明为
- 在此范围内未声明“root” - 请求't-&gt;'中的成员'left',这是非类型'int' - 请求't-&gt;'中的成员'right',这是非类型'int' - 请求't-&gt;'中的成员'element',这是非类型'int'

我做错了什么?我认为公共继承仍然可以让我访问受保护的方法和变量,并假设结构也是如此。我通过将protected更改为public来检查这是否是唯一的问题,但仍会弹出相同的错误。

我做错了吗?我对c ++很陌生,因为我正在上课,因此我更习惯于ruby和java。

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:2)

您在每个课程结束时都缺少;

class BinarySearchTree {
    ...
}; // <-- there

privateMethod1也缺少返回类型。也许是void

void privateMethod1()
{...}