7

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?

2
  • 4
    (*nptr)[i] should do the trick. The problem is that you defined operator[] to work on node class, not on pointer to node. When you say nptr[i] you're calling the built-in operator[].
    – jrok
    Sep 16, 2012 at 7:30
  • Oh! very obvious now. thank you Sep 16, 2012 at 7:32

2 Answers 2

8

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 , I should point out that the correct way to refer to the null pointer is nullptr, not NULL.

7
  • It's not 'slightly incorrect' to use int. You can advocate std::size_t if you want, but you're selling int short.
    – Luc Danton
    Sep 16, 2012 at 7:36
  • @LucDanton What do you mean by "selling int short"?
    – jrok
    Sep 16, 2012 at 7:38
  • @LucDanton std::vector<>::operator[] expects std::vector<>::size_type, so that should be used.
    – jogojapan
    Sep 16, 2012 at 7:39
  • Signed vs unsigned types is an old debate very prone to flamewars. I'd rather not bring it up, but at the same time I'd rather not let anyone get the impression using std::size_t/size_type is the automatically correct thing to do. int or std::ptrdiff_t/difference_type certainly have their merits. I'm leaving it at that.
    – Luc Danton
    Sep 16, 2012 at 7:41
  • oh! syntax mistake. That makes sense now- I can see why. Use nullptr since it's safer. understood. this post: stackoverflow.com/questions/918567/size-t-vs-containersize-type says that container::size_type is better for portability, but I've never even heard of std::ptrdiff_t/difference_type. This is just a minor issue in my code, but something to look into. thanks guys. Sep 16, 2012 at 7:44
1

Even though it is indeed legal to have operator[] return a pointer, it is better design (and fits expectations from standard classes) to return a reference instead. You can then take the address of that reference as follows:

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

and then use it as:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.