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 8556983
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:28:20+00:00 2026-06-11T15:28:20+00:00

In my class, I have a member variable std::vector<node*> children I want to overload

  • 0

In my class, I have a member variable std::vector<node*> children
I want to overload the subscript operator so that I can easily index one of the nodes.


Here is my class deceleration for that function:

node* operator[](int index);  

Here is my class definition for that function:

node* class_name::operator[](int index){

    return children[index];
}  

However, this function does not seem to return a pointer as I had hoped.
Here is the function that is giving me trouble:

void Print_Tree(node* nptr, unsigned int & depth){

    if (NULL == nptr) {
        return;
    }
      //node display code

    for (int i = 0; i < nptr->Number_Of_Children(); ++i){
        Print_Tree(nptr[i],depth+1); //<- Problem Here!
    }
     //node display code

    return;
}  

The error I get is:

error: cannot convert ‘node’ to ‘node*’ on the recursive call

I don’t understand why it gives me back a node when I want a pointer to a node.
Is there something wrong with my overloaded function?
I tried dereferencing the node in the recursive call:

Print_Tree(*nptr[i],depth+1);  
Print_Tree(*(nptr[i]),depth+1);
Print_Tree(nptr->[i],depth+1);

to no avail!

What am I doing wrong?

  • 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-11T15:28:22+00:00Added an answer on June 11, 2026 at 3:28 pm

    Your are looking for the problem in the right place, but the syntax in your three correction attempts is still slightly wrong.

    nptr is a pointer to a Node object, so you cannot apply the index operator directly (if you do, the compiler will assume it points to the beginning of a Node array and jump to the ith entry).

    Instead you need to first dereference the pointer, and then apply the index operator. Use parentheses to determine the order of this:

    Print_Tree((*nptr)[i],depth+1);
    

    On a separate note, your using int as the data type for the index into the vector is slightly incorrect. Better use std::size_t or std::vector<Node*>::size_type.


    Furthermore, given that this question is tagged c++11, I should point out that the correct way to refer to the null pointer is nullptr, not NULL.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my class, I have a member variable std::vector<node*> children Does the following class
I have a member variable that in a class that I want to make
I have a class that has a vector member variable, which I fill up
I have a class with a static std::map member variable that maps char s
let's say i want to have a member variable for a pointer to std::vector
Imagine a class C that has a member variable m_MyList of type std::vector in
I have a class Message that has a std::string as a data member, defined
Lets say I have a class with a member variable: std::unordered_map<KeyType, std::shared_ptr<ValueType>> myMap and
So I have a class that has the following member variables. I have get
Is there a way to require that a class have a particular abstract member?

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.