【发布时间】:2015-12-05 18:51:41
【问题描述】:
我有这个类,它在创建链表时很像节点。
class Element {
public:
int id;
int value;
bool parent;
bool is_ministry;
int children_count;
int children_in;
Element **children; //CHILDREN ARRAY
Element* next; //TO NOT LOSE ELEMENTS
Element(int _id,int _value,int _children_count=0,bool _is_ministry=false){
this->id=_id;
this->value=_value;
this->is_ministry=_is_ministry;
this->children_in=0;
if(_children_count>0)
this->parent=true;
else this->parent=false;
this->children_count=_children_count;
this->next=NULL;
if(_children_count>0){
this->children = new Element*[_children_count];
}
else this->children=NULL;
}
~Element(){
///delete children;
}
};
我需要这个对象有一个指向相同类型对象的指针数组,数组大小因给定的输入而变化 - children_count。 可以静态创建吗?从文件中读取值。我选择了动态方法,但我不确定它是否正确完成,因为它可以工作,但是在我添加 3 个对象后,整个事情都烧毁了。所以我在寻找合理的错误。 我正在制作类似树的东西。其中一个元素可以直接访问同一类型的对象的下一级。 编辑:更多代码
void chain_together(Element *_parent, Element *_child){
///CHILDREN_IN is and int which shows currently how much elements are in the array.
if(_parent->children_in>0){
for(int i=0;i<_parent->children_in;i++) ///CHEKING IF THERE ALREADY IS A LINK BETWEEN THEM
if(_parent->children[i]->id != _child->id){
_parent->children[_parent->children_in] = _child;
_parent->children_in++;
}
}else{
_parent->children[_parent->children_in] = _child;
_parent->children_in++;
}
}
【问题讨论】:
-
有两件事要调查:您的开发环境的调试器和std::vector
-
children应该是矩阵吗?指向指针的指针表明您需要类似子矩阵 (2D) 的东西。这对我来说没有意义。 -
我不允许使用矢量。
-
不幸的是,这里没有足够的代码让我们看到你在创建元素后如何使用它,所以我们必须假设最坏的情况。这种假设的缺点是解决方案集是天文数字。 Please provide an MCVE.
-
更多您没有向我们展示的代码。对不起,但我不得不假设这是错误的。
标签: c++ arrays oop pointers memory-management