Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 28th, 2006, 5:11 PM   #1
cwl157
Professional Programmer
 
Join Date: Feb 2005
Posts: 344
Rep Power: 4 cwl157 is on a distinguished road
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>
using namespace std;

class heapNode
{
  private:
  int value;
  heapNode *left;
  heapNode *right;

  public:
  //constructor
  heapNode(int newValue)
  {
     value = newValue;
     left = NULL; 
     right = NULL;
  }
 
  //get value
  int getValue()
  {
    return value;
  }
  
  //get right
  heapNode getRight()
  {
    return right;
  }

  //get left
  heapNode getLeft()
  {
    return left;
  }

  //set value
  void setValue(int newValue)
  {
    value = newValue;
  }

  // set right
  void setRight(heapNode *newRight)
  {
    right = newRight;
  }

  // set left
  void setLeft(heapNode *newLeft)
  {
    right = newLeft;
  }
  
}; // end of heapNode class

Quote:
program5.cpp: In member function ?heapNode heapNode::getRight()?:
program5.cpp:32: error: invalid conversion from ?heapNode*? to ?int?
program5.cpp:32: error: initializing argument 1 of ?heapNode::heapNode(int)?
program5.cpp: In member function ?heapNode heapNode::getLeft()?:
program5.cpp:38: error: invalid conversion from ?heapNode*? to ?int?
program5.cpp:38: error: initializing argument 1 of ?heapNode::heapNode(int)?
cwl157 is offline   Reply With Quote
Old Mar 28th, 2006, 5:28 PM   #2
Cache
Hobbyist
 
Join Date: Sep 2005
Posts: 261
Rep Power: 4 Cache is on a distinguished road
'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*
Cache is offline   Reply With Quote
Old Mar 28th, 2006, 7:00 PM   #3
cwl157
Professional Programmer
 
Join Date: Feb 2005
Posts: 344
Rep Power: 4 cwl157 is on a distinguished road
i think i want to return the actual value so how do i dereference them?
cwl157 is offline   Reply With Quote
Old Mar 28th, 2006, 9:15 PM   #4
Bench
Newbie
 
Join Date: Feb 2006
Posts: 20
Rep Power: 0 Bench is on a distinguished road
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;
is equivalent to
(*right).value;
But your function returns an object of type heapNode, not int.
Bench is offline   Reply With Quote
Old Mar 28th, 2006, 9:32 PM   #5
cwl157
Professional Programmer
 
Join Date: Feb 2005
Posts: 344
Rep Power: 4 cwl157 is on a distinguished road
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 value
  void insertSpot(int counter)
  {
    int arrayValue;
    bool isOne = false;
    int ar[32];

    for(int i = 0; i < 32; i++)
    {
        arrayValue = option % 2;
        option = option / 2;
        ar[i] = arrayValue;
    }

    for(int i = 0; i < 32; i++)
      cout << ar[i] << " ";
    cout << endl;

    for(int i = 31; i >= 0; i--)
    {
      if(isOne)
      {
        if(ar[i] == 0)
        {  
            //cout << "\t\t\t\tmove left\n";
        }
        if(ar[i] == 1)
        {
	  //cout << "\t\t\t\tmove right \n";
        }
      }
      if(ar[i] == 1)
        isOne = true;
    }
  }

here is the add method so far:
 // insert a new value into the heap
  insert(int value, heapNode *newNode)
  {
      if(counter == 0)
      {
        newNode = new heapNode(value);
	root = newNode;
      }
      else
      {
        insertSpot(counter)
	newNode = new heapNode(value);
      }
}
for this add method: can i just call the insertSpot method and then just make a new node like that? Don't i have to somehow say connect to the parent node somehow or something?

Here is my heapNode class that has the get methods that do not work and everything else
class heapNode
{
  private:
  int value;
  heapNode *left;
  heapNode *right;

  public:
  //constructor
  heapNode(int newValue)
  {
     value = newValue;
     left = NULL; 
     right = NULL;
  }
 
  //get value
  int getValue()
  {
    return value;
  }
  
  //get right
  heapNode getRight()
  {
    return right->right;
  }

  //get left
  heapNode getLeft()
  {
    return left->left;
  }

  //set value
  void setValue(int newValue)
  {
    value = newValue;
  }

  // set right
  void setRight(heapNode *newRight)
  {
    right = newRight;
  }

  // set left
  void setLeft(heapNode *newLeft)
  {
    right = newLeft;
  }
  
}; // end of heapNode class

Last edited by cwl157; Mar 28th, 2006 at 9:54 PM.
cwl157 is offline   Reply With Quote
Old Mar 29th, 2006, 5:59 PM   #6
Bench
Newbie
 
Join Date: Feb 2006
Posts: 20
Rep Power: 0 Bench is on a distinguished road
Quote:
I want to return the right and left pointers so i can do something to them or something?
ok... but you still haven't fixed the problems which Cache identified for you in the heapNode class..
  //get right
  heapNode getRight()
  {
    return right;
  }

  //get left
  heapNode getLeft()
  {
    return left;
  }
In case it wasn't clear to you from his post, the types of left and right are heapNode* - but your functions' signatures both return heapNode
Bench is offline   Reply With Quote
Old Mar 29th, 2006, 6:01 PM   #7
cwl157
Professional Programmer
 
Join Date: Feb 2005
Posts: 344
Rep Power: 4 cwl157 is on a distinguished road
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?
cwl157 is offline   Reply With Quote
Old Mar 30th, 2006, 4:17 PM   #8
cwl157
Professional Programmer
 
Join Date: Feb 2005
Posts: 344
Rep Power: 4 cwl157 is on a distinguished road
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>
using namespace std;

class heapNode
{
  private:
  int value;
  //heapNode *left;
  //heapNode *right;

  public:
  heapNode *left;
  heapNode *right;
  //constructor
  heapNode(int newValue)
  {
     value = newValue;
     left = NULL; 
     right = NULL;
  }
 
  //get value
  int getValue()
  {
    return value;
  }
  
  //get right
  heapNode* getRight()
  {
    return right;
  }

  //get left
  heapNode* getLeft()
  {
    return left;
  }

  //set value
  void setValue(int newValue)
  {
    value = newValue;
  }

  // set right
  void setRight(heapNode *newRight)
  {
    right = newRight;
  }

  // set left
  void setLeft(heapNode *newLeft)
  {
    right = newLeft;
  }
  
}; // end of heapNode class

class heap
{
  private:
  heapNode *root;
  int counter;

public:
  //constructor
  heap()
  {
    counter = 0;
    root = NULL;
  }

  void realInsert(int ar, int pos, int newValue)
  {
    heapNode *newNode;
    newNode = new heapNode(newValue);
    if(newNode->left == NULL)
    {
      realInsert(ar, pos-1, newValue);
      if(newNode->left->getValue() < newNode->getValue())
        newNode->setValue(newNode->left->getValue());
    }
    else if(newNode->right == NULL)
    {
      realInsert(ar, pos-1, newValue);
      if(newNode->right->getValue() < newNode->getValue())
        newNode->setValue(newNode->right->getValue());
    }
  }

  // find the spot to put new value
  void insertSpot(int option)
  {
    int arrayValue;
    bool isOne = false;
    int ar[32];
    int i = 31;
    for(int i = 0; i < 32; i++)
    {
        arrayValue = option % 2;
        option = option / 2;
        ar[i] = arrayValue;
    }
 
    for(int i = 0; i < 32; i++)
      cout << ar[i] << " ";
    cout << endl;

    while(i >= 0)
    {

      if(ar[i] == 1)
        isOne = true;
      if(isOne = true)
        break;
      i--;
    }
    realInsert(*ar, i, option);
  }

  // get root
  heapNode* getRoot()
  {
    return root;
  }

/*
    int delete(rootValue)
    {
        heapNode *tempNode = new heapNode(value, *left, *right)
        go down the heap to the last one
        make it root
        last nodeValue = null
	while(not at the bottom level of tree)
	{
	  if(newRoot < *left)
	    break;
          elseif(newRoot > *left)
	    swap with left;
        }

        return tempNode;         
     }
*/

  }; // end of heap class

int main()
{
  heap *myHeap;

  myHeap = new heap();


  int option=0;
    cout << "Please enter a number: ";
    cin >> option;
    myHeap->insertSpot(option);
  return 0;
}
cwl157 is offline   Reply With Quote
Old Mar 30th, 2006, 5:15 PM   #9
jayme
Professional Programmer
 
jayme's Avatar
 
Join Date: Nov 2005
Location: Canada
Posts: 495
Rep Power: 0 jayme is an unknown quantity at this point
Send a message via MSN to jayme
for(int i = 31; i >= 0; i--)

four posts up, first [code] tags, you know this is going to loop until i == -1, right?
__________________

Quote:
Originally Posted by Mohamed Jihad
Durka durka!
Due to incorrect calculations during the middle ages, our calendar actually begins a few years after Jesus' birth. Thus the real 6/6/6 happened a few years back. The world already ended and you missed it.

Download Code::Blocks now!
jayme is offline   Reply With Quote
Old Mar 30th, 2006, 5:39 PM   #10
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 852
Rep Power: 4 The Dark is on a distinguished road
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)
  {
    heapNode *newNode;
    newNode = new heapNode(newValue);    // Calls constructor, constructor sets value to newValue and left and right to NULL
    if(newNode->left == NULL)            // Always true due to above constructor
    {
      realInsert(ar, pos-1, newValue);  // Recursively calls this function, because we will always get to this same spot in the recusive call as well, it causes an infinite recursion, which will eventually crash.

// None of the code below here will ever get executed

      if(newNode->left->getValue() < newNode->getValue())
        newNode->setValue(newNode->left->getValue());
    }
    else if(newNode->right == NULL)
    {
      realInsert(ar, pos-1, newValue);
      if(newNode->right->getValue() < newNode->getValue())
        newNode->setValue(newNode->right->getValue());
    }
  }
The Dark is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 10:53 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC