Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   pointer help (http://www.programmingforums.org/showthread.php?t=9087)

cwl157 Mar 28th, 2006 5:11 PM

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)?

Cache Mar 28th, 2006 5:28 PM

'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*

cwl157 Mar 28th, 2006 7:00 PM

i think i want to return the actual value so how do i dereference them?

Bench Mar 28th, 2006 9:15 PM

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.

cwl157 Mar 28th, 2006 9:32 PM

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


Bench Mar 29th, 2006 5:59 PM

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

cwl157 Mar 29th, 2006 6:01 PM

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 Mar 30th, 2006 4:17 PM

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;
}


jayme Mar 30th, 2006 5:15 PM

:

for(int i = 31; i >= 0; i--)

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

The Dark Mar 30th, 2006 5:39 PM

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());
    }
  }



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