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

  • SEARCH
  • Home
  • 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 8398125
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T20:57:47+00:00 2026-06-09T20:57:47+00:00

I am iterating through the children of a tree node. The children are stored

  • 0

I am iterating through the children of a tree node. The children are stored in a ptr_vector, and at some point throughout the iteration I fall into infinite recursion, but I cannot figure out why.

Here is the method where the infinite recursion occurs (this method is just used to print the tree structure into cout):

std::ostream& operator<<(std::ostream &strm, const node &n) {
    if (n.children_.empty())
        return strm << "[]";

    for (boost::ptr_vector<node::edge>::const_iterator iter = n.children_.begin(); iter != n.children_.end(); ++iter)
    {
        if (iter != n.children_.begin())
            strm << ", ";
        strm << "-" << iter->distance << "->[ " << *(iter->dest) << "]";
    }

    return strm;
}

And here is the tree structure I am navigating (note that the purpose of the nested edge is to represent distance between parent and child nodes):

class node
{
public:
    node(void);
    ~node(void);

    node* add_child(unsigned int d);
    node* get_closest(void);

    friend std::ostream& operator<<(std::ostream&, const node&);

private:
    class edge
    {
    public:
        edge(node* n, unsigned int d);
        ~edge(void);

        unsigned int distance;
        node* dest;
    };

    boost::ptr_vector<edge> children_;
};

Additionally, I noticed that this infinite recursion occurs only after the following method has been called:

node* node::get_closest(void) const
{
    if (children_.empty())
        return NULL;

    boost::ptr_vector<node::edge>::const_iterator iter = children_.begin();
    node::edge closest = *iter;
    ++iter;

    if (iter != children_.end())
    {
        for (; iter != children_.end(); ++iter)
        {
            if (iter->distance < closest.distance)
                closest = *iter;
        }
    }

    return closest.dest;
}

Why does this method cause infinite recursion?
Thanks!

  • 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-09T20:57:48+00:00Added an answer on June 9, 2026 at 8:57 pm

    Going with @RobI’s suggestion, there mixture of raw and shared pointers is causing the problem.
    I changed node::edge closest = *iter; to const node::edge* closest = &(*iter);. Here is the new (and working) version of the method:

    node* node::get_closest(void) const
    {
        if (children_.empty())
            return NULL;
    
        boost::ptr_vector<node::edge>::const_iterator iter = children_.begin();
        const node::edge* closest = &(*iter);
        ++iter;
    
        if (iter != children_.end())
        {
            for (; iter != children_.end(); ++iter)
            {
                if (iter->distance < closest->distance)
                    closest = &(*iter);
            }
        }
    
        return closest->dest;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm iterating through some objects and trying to set some properties that, apparently, don't
I'm iterating through an array and sorting it by values into days of the
I'm iterating through an array of chars to do some manipulation. I want to
I'm terrible at iterating through arrays and could use some help. I'm using the
I'm iterating through all the children of a given Canvas object. The trouble is,
I have a has_many :through association, and I am iterating over the children to
When iterating through a set of assemblies, e.g. AppDomain.CurrentDomain.GetAssemblies(), dynamic assemblies will throw a
I'm having difficulty iterating through records while scrolling window. My initial thought was to
I'm iterating through a variable called content, it contains several HTMLLIElement objects. How can
I'm iterating through my JSON response. I've got HTML elements with id's as the

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.