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

发布于 2024-12-09 15:13:35 字数 7411 浏览 1 评论 0原文

修改为包括实际代码

我有一个

    // 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->”中的成员“left”,其属于非类类型“int”
- 请求“t->”中的成员“right”,其属于非类类型“int”
- 请求“t->”中的成员“element”,其属于非类类型“int”

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

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

任何帮助将不胜感激。

Revised to include actual code

I have a class

    // 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

that contains a struct. I have a second class "MyBST" that inherits from the above class

#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

This is my main function that will be using these two classes I am just trying to get my code to work and test it currently.

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

When I compile the two classes I get errors saying:
- ‘BinaryNode’ has not been declared
- ‘root’ was not declared in this scope
- request for member ‘left’ in ‘t->’, which is of non-class type ‘int’
- request for member ‘right’ in ‘t->’, which is of non-class type ‘int’
- request for member ‘element’ in ‘t->’, which is of non-class type ‘int’

What am I doing wrong? I thought that a public inheritance would still give me access to protected methods and variable and assumed the same would be true for structs. I checked to see if this was the only issue by changing the protected to public but the same errors still pop up.

Am I doing something wrong? I am pretty new to c++ just picked it up because of a class I am taking I am more used to ruby and java.

Any help would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

阳光的暖冬 2024-12-16 15:13:35

您在每个类的末尾缺少 ;

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

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

void privateMethod1()
{...}

You are missing a ; at the end of each of your classes.

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

Also privateMethod1 is missing a return type. Perhaps void?

void privateMethod1()
{...}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文