Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/152.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++_Oop - Fatal编程技术网

C++ 推回元素添加后消失

C++ 推回元素添加后消失,c++,oop,C++,Oop,我有一个消息类,它有一个称为child_list的消息指针向量。child_list应该包含对该主题的每个回复或对回复的回复等的地址。我将一系列消息从.txt文件导入到每个消息地址的全包消息指针向量中。然后我回到文件中,检查每封邮件的“儿童”回复。我的问题就发生在我把回复邮件的地址推回去之后。 我可以看到child_list.size随着我添加元素而增长,但是在for循环完成后,我尝试打印出消息,我看到child_string.size现在为0。为什么消息指针在我使用之前就被销毁了 //

我有一个消息类,它有一个称为child_list的消息指针向量。child_list应该包含对该主题的每个回复或对回复的回复等的地址。我将一系列消息从.txt文件导入到每个消息地址的全包消息指针向量中。然后我回到文件中,检查每封邮件的“儿童”回复。我的问题就发生在我把回复邮件的地址推回去之后。 我可以看到child_list.size随着我添加元素而增长,但是在for循环完成后,我尝试打印出消息,我看到child_string.size现在为0。为什么消息指针在我使用之前就被销毁了

    //bboard.cpp
    void Bboard::display() const
    {
        cout << endl;
        message_list[0]->print();
        cout << endl;
    }

    //add each child to the message
    for(unsigned m = 0; m < children.size(); ++m)
    {
        cout << "id to add: " << children[m] << endl;
        Message * tempPTR = message_list[ children[m]-1 ];
        cout << "tempPTR: " << tempPTR << endl;
        cout << endl;
        cout << "\tdisplay BEFORE push to message_list[id]->add_child(tempPTR): " << endl;
        display();
        message_list[id]->add_child(tempPTR);
        cout << endl;
        display();
        cout << "\tdisplay AFTER push to message_list[id]->add_child(tempPTR): " << endl;
    }
        cout << endl;
        cout << "After the for loop to add them: " << endl;
        display();

//message.cpp
void Message::add_child(Message* child)
{
    this->child_list.push_back(child);
    cout << "\tchild_list size: " << child_list.size() << endl;
}

//topic.cpp
//Message::print() is true virtual and is defined here for Topic
void Topic::print() const
{
    cout << "Message #" << this->id << ": " << this->subject << endl;
    cout << "from " << this->author << ": " << this->body << endl;
    cout << "child_list Size: " << this->child_list.size() << endl;
    //later, I need to add a for loop to display all the child messages
}
输出到终端,显示删除前的消息地址和子列表指针

从终端: 消息id:1位于地址:0x8d45870

消息id:2位于地址:0x8d45a30

消息id:3位于地址0x8d45b60

消息id:4位于地址:0x8d45da0

消息id:5位于地址:0x8d45ae0

消息id:6位于地址:0x8d45fe0

消息id:7位于地址:0x8d46170

消息id:8位于地址:0x8d46260

消息id:9位于地址:0x8d46310

消息id:10位于地址:0x8d45f90

身份证号码:1

儿童:6 9

儿童[m]:6

tempPTR:0x8d45fe0

推送至消息前显示\u列表[id]->添加\u childtempPTR:

信息1:CS12作业7

梅西:任务很难,所以一步一步来

您可以阅读提示和窍门部分以获得帮助

子列表大小:0

儿童->身份证:6

已将子项添加到列表:0x8d45fe0

子列表大小:1

信息1:CS12作业7

梅西:任务很难,所以一步一步来

您可以阅读提示和窍门部分以获得帮助

子列表大小:0

推送至消息后显示\u列表[id]->添加\u childtempPTR:

儿童[m]:9

tempPTR:0x8d46310

推送至消息前显示\u列表[id]->添加\u childtempPTR:

信息1:CS12作业7

梅西:任务很难,所以一步一步来

您可以阅读提示和窍门部分以获得帮助

子列表大小:0

儿童->id:9

已将子项添加到列表:0x8d46310

子列表大小:2

信息1:CS12作业7

梅西:任务很难,所以一步一步来

您可以阅读提示和窍门部分以获得帮助

子列表大小:0

推送至消息后显示\u列表[id]->添加\u childtempPTR:

在for循环之后添加它们:

信息1:CS12作业7

梅西:任务很难,所以一步一步来

您可以阅读提示和窍门部分以获得帮助


child\u列表大小:0

您正在使用void Message::add\u childMessage*child方法添加到列表中,然后尝试打印出一个具有void Topic::print const的列表

消息类和主题类是否通过继承相互关联?或者它们都持有对同一子列表实例的引用


从您发布的代码中,我认为有多个child_列表向量,您打印的可能是错误的?

我认为如果您在看到child_string.size==0的地方显示代码会有所帮助。此外,什么是child_列表?ints?循环使用的索引类型为unsigned;我想你指的是无符号整数。什么是id?是整数吗?更多的代码和上下文可能会有帮助。@dash:unsigned是unsigned int的同义词。我以为unsigned和unsigned int是同一回事,我错了吗?ID也都是“unsigned”类型。@MikeTrottman-为什么要在语句-Message*temptr=Message_list[children[m]-1]?此外,当成员变量在类的作用域中时,除非与调用参数和成员变量的名称冲突,否则无需对其使用此选项。此->child\u list.push\u backchild;add_child从消息继承到topid和reply对象。但是,可能它试图打印错误的子项列表。可能显示在添加子项循环和打印调用之间发生的代码“Topic”和“Reply”类都继承“Message”,而“add\u child”仅在“Message”中声明。当我调用'message_list[x]->print;'它检查该对象的“this->child\u list.size”是否仍然要求与我之前“push\u back”的“child\u list”不同的“child\u list”?如果需要,我可以发布更多的代码,我还应该添加什么?@MikeTrottman-我想一个简单的测试应该是添加一个消息列表[id]->print;在消息之前和之后调用\u list[id]->add\u childtemptr;调用以确保两个方法都引用同一对象。@Vusak“display”是调用打印命令的命令,我现在只打印“message_list”中的第一个元素。当调用“message_list->print”时,我就有了它 打印'child_list.size',以便查看该向量中是否有元素。这就是迄今为止我在尝试解决此问题时的设置方式。