Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9065555
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T16:31:51+00:00 2026-06-16T16:31:51+00:00

I’ve tried to implement an easy Node Class but when i try to access

  • 0

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?

Node::Node(QString name, Node *parent)
{
    this->name = name;
    this->parent = parent;

    parent->children.append(this);
}

\\

 class Node
    {
    public:
        Node(QString name) { this->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->parent->children.indexOf(this);}

    QString log(int tabLevel = -1);

private:
    QString name;
    QList<Node*> children;
    Node *parent;
};

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 :/

    Node rootNode = Node("rootNode");
Node childNode0 = Node("childNode0", &rootNode);
Node childNode1 = Node("childNode1", &rootNode);
Node childNode2 = Node("childNode2", &rootNode);
Node childNode3 = Node("childNode3", &childNode0);
Node childNode4 = Node("childNode4", &childNode0);

qDebug() << "RootNode: " + rootNode.getName() << " | RootChilds: " << rootNode.childCount();
qDebug() << "NodeName: " +rootNode.getChild(0)->getName() << " | NodeChilds: " << rootNode.getChild(0)->childCount();

for(int i = 0; i < childNode0.childCount(); i++)
{
    qDebug() << "NodeName: " << childNode0.getName() << " | NodeChilds: " << childNode0.childCount() << "Child Nr: " << i << " Name -> " << childNode0.getChild(i)->getName();
} 

qDebug() << "Adress via root: " << rootNode.getChild(0) << "\nAdress via node: " << &childNode0 ;

}

That outputs:

"RootNode: rootNode"  | RootChilds:  3 
"NodeName: childNode0"  | NodeChilds:  0 
NodeName:  "childNode0"  | NodeChilds:  2 Child Nr:  0  Name ->  "childNode3" 
NodeName:  "childNode0"  | NodeChilds:  2 Child Nr:  1  Name ->  "childNode4" 
Adress via root:  0x41fc84 
Adress via node:  0x41fcc0 

I hope someone can help me

Regards

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T16:31:52+00:00Added an answer on June 16, 2026 at 4:31 pm

    You’re giving the parent Node the address to a temporary Node when you do

    Node childNode0 = Node("childNode0", &rootNode);
    //                ^ rootNode gets this temporary Node as its first child
    

    Rather than constructing Nodes by copying from a temporary, do:

    Node childNode0("childNode0", &rootNode);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to select an H1 element which is the second-child in its group
I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I am doing a simple coin flipping experiment for class that involves flipping a
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I want to construct a data frame in an Rcpp function, but when I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.