如何在C ++中创建包含子类的类的链接列表?

时间:2021-07-21 07:18:36

For example, in the simple example below, how can I create an instance of "Child" that will store the information in "Parent", and continue the "Group" linked list to the next node?

例如,在下面的简单示例中,如何创建将“信息”存储在“父”中的“子”实例,并将“组”链接列表继续到下一个节点?

Entire Code: https://ideone.com/v7dohX

整个代码:https://ideone.com/v7dohX

Example Function to Add a "Child":

添加“子”的示例函数:

void Group::addChild() // Adding an instance of a child class.
{
    Parent *now = bottom, *temp;
    string name = "Jason", gender = "male";
    int age = 21;

    temp == new Child(name, age, gender);

    if (count == 0)
    {
        top = bottom = temp;
        temp->setNext(NULL); // Segmentation fault?
        count++;
    }
}

Example Classes:

class Parent // Holds the info for each node.
{
    private:
        string name;
        int age;
        string gender; // Will be specified in child class.
        Parent *next;

    public:
        Parent(string newname, int newage)
        {
            name = newname;
            age = newage;
        }

        void setGender(string myGender)  { gender = myGender; }
        void setNext(Parent *n)  { next = n; }
};

class Child : public Parent // Create instances to store name, age, and gender.
{
    public:
        Child(string newname, int newage, string newgender) : Parent(newname, newage)
        {
            setGender(newgender);
        }
};

class Group // Linked list containing all Parent/Child.
{
    private:
        int count;
        Parent *top;
        Parent *bottom;

    public:
        Group()
        {
            count = 0;
            top = bottom = NULL;
        }

        void addChild(); // Defined in the function listed at the top of this post.
};

When I run my code, I get a segmentation fault. How can I perform this simple task?

当我运行我的代码时,我遇到了分段错误。我该如何执行这个简单的任务?

1 个解决方案

#1


There are a couple of important flaws in this code. As Joachim commented, you didnt assign temp, you compared.

这段代码中有几个重要的缺陷。正如约阿希姆评论的那样,你没有分配温度,你进行了比较。

It should be:

它应该是:

temp = new Child(name, age, gender);

temp = new Child(姓名,年龄,性别);

But also, your constructor for Parent should initialize next. Learn initializer list syntax too. Here is a possible parent constructor

但是,您的Parent构造函数应该初始化。也学习初始化列表语法。这是一个可能的父构造函数

Parent(string newname, int newage, Parent* n = NULL) : name(newname), age(newage), next(n) { }

Parent(字符串newname,int newage,Parent * n = NULL):name(newname),age(newage),next(n){}

You are not setting it right away, and that is asking for trouble. If you forgot to setNext() (and right now you are only doing so when the list is empty) then you have a dangling pointer. The next pointer will point to a random location in memory, not null, and you will crash when you go there.

你没有马上设置它,这就是要求麻烦。如果你忘了setNext()(现在你只是在列表为空时才这样做)那么你有一个悬空指针。下一个指针将指向内存中的随机位置,而不是null,当你去那里时你会崩溃。

#1


There are a couple of important flaws in this code. As Joachim commented, you didnt assign temp, you compared.

这段代码中有几个重要的缺陷。正如约阿希姆评论的那样,你没有分配温度,你进行了比较。

It should be:

它应该是:

temp = new Child(name, age, gender);

temp = new Child(姓名,年龄,性别);

But also, your constructor for Parent should initialize next. Learn initializer list syntax too. Here is a possible parent constructor

但是,您的Parent构造函数应该初始化。也学习初始化列表语法。这是一个可能的父构造函数

Parent(string newname, int newage, Parent* n = NULL) : name(newname), age(newage), next(n) { }

Parent(字符串newname,int newage,Parent * n = NULL):name(newname),age(newage),next(n){}

You are not setting it right away, and that is asking for trouble. If you forgot to setNext() (and right now you are only doing so when the list is empty) then you have a dangling pointer. The next pointer will point to a random location in memory, not null, and you will crash when you go there.

你没有马上设置它,这就是要求麻烦。如果你忘了setNext()(现在你只是在列表为空时才这样做)那么你有一个悬空指针。下一个指针将指向内存中的随机位置,而不是null,当你去那里时你会崩溃。