Note that there are some explanatory texts on larger screens.

plurals
  1. PONode Class logic mistake somewhere?
    text
    copied!<p>I've tried to implement an easy Node Class but when i try to access the child node via the root node its empty, but if i access the child node directly it's filled correctly. Do i have somewhere a logic mistake or can some one give me a hint whats wrong?</p> <pre><code>Node::Node(QString name, Node *parent) { this-&gt;name = name; this-&gt;parent = parent; parent-&gt;children.append(this); } </code></pre> <p>\\</p> <pre><code> class Node { public: Node(QString name) { this-&gt;name = name; } Node(QString name, Node *parent); ~Node(void); void addChild(Node *child); QString getName() { return name; } Node* getChild(int row) { return children[row]; } Node* getParent() { return parent; } int childCount() { return children.size(); } int getRow() {return this-&gt;parent-&gt;children.indexOf(this);} QString log(int tabLevel = -1); private: QString name; QList&lt;Node*&gt; children; Node *parent; }; </code></pre> <p>I tried to find the mistake and my result is, that the child node seems to have two different addresses, so there are two different objects but i don't know why :/</p> <pre><code> Node rootNode = Node("rootNode"); Node childNode0 = Node("childNode0", &amp;rootNode); Node childNode1 = Node("childNode1", &amp;rootNode); Node childNode2 = Node("childNode2", &amp;rootNode); Node childNode3 = Node("childNode3", &amp;childNode0); Node childNode4 = Node("childNode4", &amp;childNode0); qDebug() &lt;&lt; "RootNode: " + rootNode.getName() &lt;&lt; " | RootChilds: " &lt;&lt; rootNode.childCount(); qDebug() &lt;&lt; "NodeName: " +rootNode.getChild(0)-&gt;getName() &lt;&lt; " | NodeChilds: " &lt;&lt; rootNode.getChild(0)-&gt;childCount(); for(int i = 0; i &lt; childNode0.childCount(); i++) { qDebug() &lt;&lt; "NodeName: " &lt;&lt; childNode0.getName() &lt;&lt; " | NodeChilds: " &lt;&lt; childNode0.childCount() &lt;&lt; "Child Nr: " &lt;&lt; i &lt;&lt; " Name -&gt; " &lt;&lt; childNode0.getChild(i)-&gt;getName(); } qDebug() &lt;&lt; "Adress via root: " &lt;&lt; rootNode.getChild(0) &lt;&lt; "\nAdress via node: " &lt;&lt; &amp;childNode0 ; </code></pre> <p>}</p> <p>That outputs:</p> <pre><code>"RootNode: rootNode" | RootChilds: 3 "NodeName: childNode0" | NodeChilds: 0 NodeName: "childNode0" | NodeChilds: 2 Child Nr: 0 Name -&gt; "childNode3" NodeName: "childNode0" | NodeChilds: 2 Child Nr: 1 Name -&gt; "childNode4" Adress via root: 0x41fc84 Adress via node: 0x41fcc0 </code></pre> <p>I hope someone can help me</p> <p>Regards</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload