C++ 构造函数C+的超类与子类继承+;

C++ 构造函数C+的超类与子类继承+;,c++,inheritance,constructor,subclass,base,C++,Inheritance,Constructor,Subclass,Base,这是一个包含左、右、父和数据的二叉搜索树的基类 template<class Data> class BSTNode { public: /** Constructor. Initialize a BSTNode with the given Data item, * no parent, and no children. */ BSTNode(const Data & d) : data(d) { left

这是一个包含左、右、父和数据的二叉搜索树的基类

template<class Data>
class BSTNode
{
public:

    /** Constructor.  Initialize a BSTNode with the given Data item,
     *  no parent, and no children.
     */
    BSTNode(const Data & d) : data(d)
    {
        left = right = parent = 0;
    }


    BSTNode<Data>* left;
    BSTNode<Data>* right;
    BSTNode<Data>* parent;
    Data const data;   // the const Data in this node.

    /** Return the successor of this BSTNode in a BST, or 0 if none.
     ** PRECONDITION: this BSTNode is a node in a BST.
     ** POSTCONDITION:  the BST is unchanged.
     ** RETURNS: the BSTNode that is the successor of this BSTNode,
     ** or 0 if there is none.
     */
    BSTNode<Data>* successor()
    {
        BSTNode<Data>* cursor;
        BSTNode<Data>* par;
        cursor = this->right;
        par = this->parent;

        if (this->right != NULL)
        {
            while (cursor->left != NULL) {
                cursor = cursor->left;
            }
            return cursor;
        }
        if ((this->right == NULL) &&  (this == par->left))
            return this->parent;

        if ((this->right == NULL) && (this == par->right))
        {
            do
            {
                cursor = par;
                par = par->parent;
                if (par ==  NULL)
                {return cursor;}
            } while(cursor != par->left);
            return par;
        }
        if (this->right == NULL && this->parent == NULL)
            return NULL;

        return NULL;
    }
};
模板
类节点
{
公众:
/**构造函数。使用给定的数据项初始化BSTNode,
*没有父母,也没有孩子。
*/
BSTNode(常量数据和d):数据(d)
{
左=右=父=0;
}
节点*左;
节点*对;
BSTNode*父节点;
Data const Data;//此节点中的const数据。
/**在BST中返回此BSTNode的后续节点,如果没有,则返回0。
**前提条件:此BST节点是BST中的节点。
**后置条件:BST不变。
**返回:作为此BSTNode的后续节点的BSTNode,
**如果没有,则为0。
*/
BSTNode*后继者()
{
节点*光标;
BSTNoNo.*PAR;
光标=此->右侧;
PAR=->父代;
如果(此->右侧!=NULL)
{
while(光标->左!=NULL){
光标=光标->左;
}
返回光标;
}
如果((此->右==NULL)和(&(此==par->左))
返回此->父项;
如果((此->右==NULL)和(&(此==par->right))
{
做
{
光标=PAR;
PAR=PAL>亲本;
如果(par==NULL)
{返回游标;}
}同时(光标!=par->left);
返回票面金额;
}
if(this->right==NULL&&this->parent==NULL)
返回NULL;
返回NULL;
}
};
子类是RSTNode,它应该使用BSTNode的所有成员,并在上面添加优先级:

template<class Data>
class RSTNode: public BSTNode<Data>
{
public:
    int priority;

    RSTNode(Data const & d)
        : BSTNode<Data>(d)
    {
        //call a random number generator to generate a random priority
        priority = rand();
    }
};
模板
类RSTNode:公共BSTNode
{
公众:
int优先级;
RSTNode(数据常量和数据)
:BSTNode(d)
{
//调用随机数生成器生成随机优先级
优先级=兰德();
}
};

现在的问题是,我不确定如何实现RSTNode的构造函数,因为出于某种原因,它无法识别BSTNode的成员。我知道它应该识别它们,因为它应该继承这些信息。欢迎提供任何帮助。

这是因为数据成员的默认范围是
private
。如果要在子类中访问它们,需要将它们声明为受保护的


更好的方法是,在
BSTNode
中添加一个构造函数,允许您传入初始化值,并从
RSTNode
构造函数中调用它,因为它应该是管理其成员生存期的基类。

如果我读得正确,您尝试从模板类继承时:

class RSTNode: public BSTNode<Data>
这是问题的一部分还是粘贴了错误的代码

您可以通过模板化BSTNode或

template <typename T> class BSTNode {
然而您试图理解您所写内容的事实意味着您没有真正理解更深层次的类定义,事实上,您正试图直接在派生类构造函数中设置基类参数,并且您已经公开了这些参数,这将使我相信您需要更多地了解面向对象设计——因此,尽管这可能会从技术上解决您的问题,但对于您遇到的问题来说,这只是冰山一角


另外说“它由于某种原因无法识别BSTNode的成员”也没有多大帮助,因为我怀疑这不是您尝试执行此操作时编译器的确切输出。

好的,我是在Visual Studio中编译的

template<class Data>
class BSTNode
{
public:

    /** Constructor.  Initialize a BSTNode with the given Data item,
     *  no parent, and no children.
     */
    BSTNode(const Data & d) : data(d)
    {
        left = right = parent = 0;
    }


    BSTNode<Data>* left;
    BSTNode<Data>* right;
    BSTNode<Data>* parent;
    Data const data;   // the const Data in this node.
};

template<class Data>
class RSTNode : public BSTNode<Data>
{
public:
   int priority;

   RSTNode(Data const & d)
      : priority(rand()),
        BSTNode<Data>(d)
   {
      left = 0; //Accessible because public
      right = 0;
      parent = 0;
   }
};

int _tmain(int argc, _TCHAR* argv[])
{
   RSTNode<std::string> node(std::string("test"));
    return 0;
}
然后在RSTNode中使用它,或者在RSTNode中使用一个类似的RSTNode,并传递给该RSTNode

RSTNode(Data const & d, BSTNode* l, BSTNode* r, BSTNode* p)
   : priority(rand()),
     BSTNode<Data>(d,l,r,p)
{
}
RSTNode(数据常量&d,BSTNode*l,BSTNode*r,BSTNode*p)
:优先级(rand()),
BSTNode(d、l、r、p)
{
}
希望这有点帮助,请注意,您应该更喜欢初始化列表,而不是直接访问ctor中的成员。但是如果您不能更改基类,那么您需要



更正输入错误-数据->数据

此代码看起来与此问题中的代码非常相似:我包含了更多代码^^^^我的类声明前面确实有此代码:
模板
不确定为什么没有将我的代码放在上面进行复制。如果这是个问题,我很抱歉。
template<class Data>
class BSTNode
{
public:

    /** Constructor.  Initialize a BSTNode with the given Data item,
     *  no parent, and no children.
     */
    BSTNode(const Data & d) : data(d)
    {
        left = right = parent = 0;
    }


    BSTNode<Data>* left;
    BSTNode<Data>* right;
    BSTNode<Data>* parent;
    Data const data;   // the const Data in this node.
};

template<class Data>
class RSTNode : public BSTNode<Data>
{
public:
   int priority;

   RSTNode(Data const & d)
      : priority(rand()),
        BSTNode<Data>(d)
   {
      left = 0; //Accessible because public
      right = 0;
      parent = 0;
   }
};

int _tmain(int argc, _TCHAR* argv[])
{
   RSTNode<std::string> node(std::string("test"));
    return 0;
}
BSTNode(const Data & d, BSTNode* l, BSTNode* r, BSTNode* p) 
       : data(d),
       left(l),
       right(r),
       parent(p)
    {
    }
RSTNode(Data const & d, BSTNode* l, BSTNode* r, BSTNode* p)
   : priority(rand()),
     BSTNode<Data>(d,l,r,p)
{
}