Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/161.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何在c+中将值从超类传递到子类+;_C++_Inheritance_Tree - Fatal编程技术网

C++ 如何在c+中将值从超类传递到子类+;

C++ 如何在c+中将值从超类传递到子类+;,c++,inheritance,tree,C++,Inheritance,Tree,我创建了一个名为“tree”的超类。我已经在这节课上构建了这棵树。现在,我想把构造树的根传递给另一个类,它是树的子类。但是当我试图传递它时,子类调用supercalss构造函数并将其设置为NULL struct node { struct node *left; struct node *right; int val; }; struct node *create(int val) { struct node *temp = (struct node *)mall

我创建了一个名为“tree”的超类。我已经在这节课上构建了这棵树。现在,我想把构造树的根传递给另一个类,它是树的子类。但是当我试图传递它时,子类调用supercalss构造函数并将其设置为NULL

struct node
{
    struct node *left;
    struct node *right;
    int val;
};
struct node *create(int val)
{
    struct node *temp = (struct node *)malloc(sizeof(struct node));
    temp->val = val;
    temp->left = temp->right = NULL;
    return temp;
};
class tree
{
    public:
        struct node *root;
        tree()
        {
            root = NULL;

        }
        void createtree()
        {
            root = create(5);
        }
        void preorder()
        {
            preorderp(root);
        }
        void preorderp(struct node *p)
        {
            if(!p) {
                return;
            }
            cout<<p->val<<' ';
            preorderp(p->left);
            preorderp(p->right);
        }
};
struct节点
{
结构节点*左;
结构节点*右;
int-val;
};
结构节点*创建(int val)
{
结构节点*temp=(结构节点*)malloc(sizeof(结构节点));
温度->瓦尔=瓦尔;
临时->左=临时->右=空;
返回温度;
};
类树
{
公众:
结构节点*根;
树()
{
root=NULL;
}
void createtree()
{
根=创建(5);
}
空预订单()
{
前序(根);
}
void预订单(结构节点*p)
{
如果(!p){
返回;
}

cout首先,你不应该让root has
public
。这是一个严重的OO错误。如果你想让它对子类可用,你应该让它受到
保护

因为继承,每个
树操作符
也是一个
。这意味着

treeiterator treeIt;
treeIt.createtree();
将执行OP想要的操作。无需创建单独的
并移动

但是,在C++世界中这有点奇怪,因为OP在使用构造函数。例如,<代码>节点< /C> >可以是:

struct node
{
    node *left;
    node *right;
    int val;

    node(int inval): 
        val(inval), 
        left(nullptr), 
        right(nullptr)
        // the above is a Member Initializer List. It makes sure all of your 
        // members are initialized before the body of the constructor runs.
    {
    }
};
构造函数中
后面的位是。 现在,当您分配
节点时,它已初始化并准备好链接。对于

class tree
{
    public:
        struct node *root; // almost certainly should not be public.
                           // also should be a std::unique_ptr<node>
        tree(int inval)
        {
            root = new node(5); // note new in place of malloc. new allocates 
                                // storage and calls constructors. malloc should 
                                // only be used in C++ in rare edge-cases.
        }
        /* obsolete
        void createtree()
        {
            root = create(5);
        }
        */
        ...
};
现在,分配一个
treeiterator
可以保证它已准备就绪,无需进一步工作

treeiterator treeIt(5); // all done.
以上所有内容都包含在本文中。我建议您购买一本并阅读它,因为现在您似乎正在尝试编写糟糕的C

非专题1:

你会很快发现这个代码违反了三条规则。如果你不知道,请阅读链接。这将节省你很多时间和精力

非专题2:

#include <bits/stdc++.h>
using namespace std;
#包括
使用名称空间std;

是一个定时炸弹。第一行包括整个标准库,但只有在GCC中。你的代码现在做的远远超过它需要编译的工作量,不再是标准的C++,它不可移植到其他编译器,也可以很好地与GCC的下一个版本相违背。不要在<代码> Bit < /C>中使用任何东西。它是内部编译器规范。没有任何保证的虚假的东西

详情如下:

第二行获取
std
命名空间中的所有内容并将其放置在全局命名空间中。这会导致调用is
reverse
std::reverse
等有趣的游戏?通常这会导致疯狂和神秘的编译器消息,因为糟糕的编译器会像地狱一样困惑,但有时它不会困惑和错误在众多的选择中,最好的选择就是默默地破坏其他东西。调试非常有趣

详情如下:

同时,您将整个标准库拉入您的文件中,并删除其适当的命名空间。这将导致潜在的隐藏痛苦形成一个巨大的雷区,不值得任何时间节约。由此产生的错误之一可能比多年来为每个文件和字符多键入几行所需的清理成本更高


没有人想用这个愚蠢的错误来清理代码,所以在专业环境中这样做可能会很昂贵。

为什么要使用
malloc()在C++中代替<代码>新< /代码>不应该。<代码>根>代码>公开,等待重读。我想我看到了。当创建<代码>树eTeaReals/Cuth>时,它内置了<代码>树>代码>。不需要建立<代码>树< /代码>。您需要在<代码>树eTeRealths<代码>下调用<代码> CurtTeReE < /代码>。在构造函数中执行初始化。@user4581301:你能详细解释一下吗?你似乎弄乱了实例和类继承。你代码中的
树的每个实例都有自己的
。你应该发布一个完整的编译示例来澄清你的说法。@Johan:include我读了完整的代码。
class treeiterator:public tree
{
    struct node *p; // Don't see the point off this
    stack<struct node *> s; // or this, but that's another question
    public:
        treeiterator(int inval): 
            tree(inval) // call's tree's constructor
        {   
        }
        bool hasnext();
        int next();
    private:
        void push(struct node *root);
};
treeiterator treeIt(5); // all done.
#include <bits/stdc++.h>
using namespace std;