I don't quite understand... current and data are initialized. Current points to root, and if root was null it would never go into the while loop. So there root points to data, and that data is an object. The object has a function called getAmount() which returns an int.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
Node *current = root;
while(current != NULL && amt != current->data->getAmount())
{
if(amt < current->data->getAmount())
{
current = current->left;
}
else
{
current = current->right;
}
}
Yes. From your code it is obvious that current is always not null, but that does not mean that it is initializated properly. Also there is no checks for data. Another possibility is that getAmount() manipulates pointers incorrectly.
Your error clearly states that you tried to dereference invalid pointer. Now you only need to find where this invalid pointer slipped in your program.
If you post whole program we might be able to debug it for you.
The whole program is too large: it consists of 2 classes and a large main. It is also a homework assignment so I don't think I can post the whole thing.
I did however find the error in the assignment. Before the while loop, I checked if current->data == NULL, and I got the exception again. So, the problem is with current->data.
The only function that runs before this inserts a node into the tree. One node has already been inserted into the tree by the time my problem occurs. the code for my insertion is this:
So when I constructed a Tree in my main, it was made with a Node root pointing to Null. Then I constructed my tree so far by adding one newNode. Now, pointing to the data of that newNode causes errors.
I suspect the problem is that current->data is NULL.
It just points to NULL right?
Only if Node's default constructor initializes it that way.
I'm also worried about Tree::Insert(). If newData isn't already in the tree then you insert it and the tree takes ownership of it. But if newData is already in the tree then who owns it?
I set the left and right to NULL, and I got the exception.
The line I'm getting the exception on is when I call current->data->getAmount();, and when it gets to the "getAmount()" function in another class. When it's inside that other function, the program crashed when it tries to return an int.
I find that if I called current->data->getAmount(); anywhere it doesn't work. Can I even legally do "current->data->getAmount();"? If I was doing something else, like "current->data->amount;" I would have to put it in parenthesis and dereference it to get the "amount": *(current->data->amount). So is there anything I have to do if I'm calling a function?
Okay, then here is my code where I create the item before inserting it into the tree:
1 2
Item newItem( stoi(line)); // the numbers were in string line, so I converted from string to int
Tree.Insert(&newItem); // The function to insert in tree takes in a new Item as a pointer so I passed in a reference
Again, you need to be clear on ownership of the items. In this case, you're inserting a local variable into a tree structure. As soon as newItem goes out of scope, the tree contains a dangling pointer (pointer to invalid memory). E.g.:
1 2 3 4 5 6 7
{
Item newItem( stoi(line));
Tree.Insert(&newItem);
// newItem gets destroyed. Tree is now corrupt.
}
Item *secondItem = new Item(10);
Tree.Insert(secondItem);
Line 7 will result in undefined behavior as the tree tries to compare secondItem to the now destroyed newItem.
So are what are some ways I can fix this issue? I've only started learning C++ for the last month, and we just got onto trees and nodes, which we are doing at the same time. It is very confusing.