Overload index operator does not return a pointer

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


Here is my slowdown class for this function:

node* operator[](int index);  

Here is my class definition for this 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 a function that gives me problems:

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 recursive call

I don’t understand why it returns node to me when I want a pointer to node.
Is there something wrong with my overloaded function?
I tried dereferencing node in a 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?

+5
2

, - .

nptr Node, ( , , Node i- ).

, . :

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

int . std::size_t std::vector<Node*>::size_type.


, , , , nullptr, NULL.

+7

, operator[] , ( ), . :

node& class_name::operator[](int index){
    return *(children[index]);
}

:

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

All Articles