从派生类访问基类中的结构体
C++ accessing a struct in a base class from derived 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
包含一个结构体。我有第二个类"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 '未在此范围内声明
-请求成员' left '在' t-> '中,非类类型' int '
-请求成员' right '在' t-> '中,它是非类类型' int '
-请求成员' element '在' t-> '中,非类类型' int '
我做错了什么?我认为公共继承仍然可以让我访问受保护的方法和变量,并假设对结构体也是如此。我检查了一下,看看这是否是唯一的问题,将protected更改为public,但仍然弹出相同的错误。
我做错了什么吗?我对c++很陌生,因为我正在上一门课,所以我比较习惯ruby和java。
您在每个类的末尾都缺少一个;
。
class BinarySearchTree {
...
}; // <-- there
privateMethod1
也缺少返回类型。可能是void
?
void privateMethod1()
{...}
相关文章:
- std::具有相同基类的类的变体
- 结构体和类的不同大小(),彼此具有相同的字段类型
- 相同的层次结构,访问基类的受保护成员时的行为不同
- 基类类型向量中的派生结构
- 结构根据空基类顺序更改大小
- 从派生类重新定义基类中定义的结构
- 是否可以将基类的结构分配给派生类的结构
- 是否有希望在std::变体上高效地调用一个公共基类方法
- 将initalizer列表与从空基类继承的结构一起使用
- 结构绑定:与公共数据成员(继承的基类)结合
- 从具有静态结构的基类派生
- 枚举器、结构体和类之间有什么区别?
- 为什么结构体和类都存在于C++中
- 模板元编程时结构体和类之间的区别
- c++从结构体到类的变化
- 从另一个基类向结构体添加数据成员
- 构造一个内部有结构体的类
- 从派生类访问基类中的结构体
- 为什么将此 POD 结构用作基类会很危险
- 位操作- c++中对结构体和类的按位操作