Problem with increment operator

I have two classes. In the first I create a node with left right pointers, a templated data member (<T> data;) and a int count;.

In my binary search tree class, when I do insert and find the word already there, I want to increment count by 1. (Count is defaulted to 1 in the constructor.)

I put in this function in the node class.

node<T> operator++() {
count++;
return *this;
}

then I also put in the other

node<T> operator++(int) {
node<T> temp = *this;
count++;
return temp;
}

Now in my BST the insert is working, and it's putting the word in, and my program gets to the point of actually incrementing the count value.

I have a node<T> curr which is equal to the node I have found the word in.

if(data = curr->data) {
curr++;
return true; // successful insert.
}

Curr is the proper type (a node) but when I try to use the post or prefix operator, it never actually enters those functions. I can however change curr++ to curr.count+=1; and it works.

Is it not possible to call the increment overload from inside another class? do I need to friend that operator or something?


This if(data = curr->data) { looks like your're using a pointer. Then you have to write (*curr)++; otherwise you'd increment the pointer.

You do realize that this code does two things, right?

1
2
3
4
if(data = curr->data) {
  curr++;
  return true; // successful insert. 
}


It assigns curr->data to data and then executes the stuff inside the braces if data is not NULL.

If you really want to compare data with curr->data, you will need double equals ==
Last edited on
Thanks for the pointer information! That was the problem!

I had the == in the code but typed it wrong when I put it on the page here. Appreciate the help!
Topic archived. No new replies allowed.