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

标签 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
    }
};

新编辑

这是我的主要功能,将使用这两个类,我只是想让我的代码正常工作并在当前进行测试。

#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->'中的成员'left',它是非类类型'int'
- 请求't->'中的成员'right',它是非类类型'int'
- 请求't->'中的成员'element',它是非类类型'int'

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

我做错了什么吗?我对 C++ 还很陌生,只是因为我正在上一门课才学会它,我更习惯于 ruby​​ 和 java。

如有任何帮助,我们将不胜感激。

最佳答案

您在每个类(class)的末尾都缺少 ;

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

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

void privateMethod1()
{...}

关于C++从派生类访问基类中的结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7761564/

相关文章:

c++ - C++遍历一般树

c++ - 与 Java 接口(interface)相比,C++ 中的接口(interface)和虚函数

c++ - 与 std::bind 相对,为给定参数传入不同的函数

c++ - 是否有带有语法突出显示的命令行 C++ 到 PDF 转换器?

c++ - 构造函数继承和自定义构造函数

python - 使用多重继承调用父类 __init__,正确的方法是什么?

java - 为什么一个接口(interface)不能实现另一个接口(interface)?

c - 是否允许对 "compatible"结构体的结构体指针进行转换和取消引用?

c++ - 读取带分隔符的文件

c - C 结构体成员的默认值