![]() |
pointer help
alright so i have 2 pointers pointing to the left and right child nodes of a heap. I have only done the heapNode class so far. When i compile it gives me some errors. I know the errors are the same problem just once for the right and once for the left. I am pretty sure they are in the get methods for the left and right child. I will post the code and the errors.
:
#include <iostream>Quote:
|
'left' and 'right' are of type heapNode*. So, if you want to return their value you will have to dereference them. If you want to return the actuall pointer you need to change the return type of the accessor members to: heapNode*
|
i think i want to return the actual value so how do i dereference them?
|
Before dereferencing any pointer, check that it is not NULL.
You most probably don't want to return heapNode by value, because this uses copying. Cache's suggestion of returning the pointer is probably closer to what you need. If you want to access the int value data member contained in the pointed-to-object, you can use the -> operator. :
right->value;:
(*right).value; |
yea because i have one that returns the value. I want to return the right and left pointers so i can do something to them or something?i dont know all i know is i am trying to add things to a min heap using nodes and need to have a method to access the left and right children of the node. In the constructor i set them to null becuase there is nothing attached to them. I do not even know how to attach a new node to them. I will post the program and if someone has some idea on how i can add a new node and make it work that would be appreciated. Right now i have a counter that goes to the place where the new node is supposed to go but can i just say to make a new node? I somehow need to make the new node connect to the right or left child of the previous node or something? The counter is wierd. It just counts how many have been entered and then take the binary number of that and for 0 moves left and 1 moves right. However, right now i just have it saying move left or move right instead of actually moving. How do i make it where it actually moves and enters the node in the right spot?
Here is the method that finds a spot: :
// find the spot to put new valuehere is the add method so far: :
// insert a new value into the heapHere is my heapNode class that has the get methods that do not work and everything else :
class heapNode |
Quote:
:
//get right |
i am still confused on how to fix the problem. So to get the thing that right points to i need to set the return type of those functions to heapNode*? And that will correctly return the node right or the node left points to?
|
ok so now i think i have the whole input done but when i try to enter a number it prints the array on which way to go ok and then i get a segmentation fault. Here is the code:
:
#include <iostream> |
:
for(int i = 31; i >= 0; i--)four posts up, first [code] tags, you know this is going to loop until i == -1, right? |
If you trace through your realInsert code, you can see where it will crash (comments in red):
:
void realInsert(int ar, int pos, int newValue) |
| All times are GMT -5. The time now is 4:44 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC