mardi 1 juin 2021

C++ pointer in a class [closed]

I am having some trouble with a pointer. I have been working on the exact same idea for almost a week, keep doing the same thing, same error, trying with different ideas but still I am not able to make it work.

Someone has answered my questions saying that creating a pointer in a function will not modify the actual value of the pointer is pointing. However, I have made it work in one of my code below (please refer to second block of code).

PROBLEM that I am having: When I try to print the values, I have a "segmentation fault: 11" error, which means I am not setting the value to the actual root.

Here is the code.

First block of code:

void tree::insert(const int x){

    if(root == nullptr)
    {
        root = new node(x);
    }
    else{

        auto current = getRoot();

        while(current != nullptr){
            if(current->data > x)
            {
                if(current->leftChild == nullptr){
                    current = new node(x);
                    cout << current->data << endl;
                    root = current;
                    break;
                }
                else{
                   current = current->leftChild;
                }
            }
            else if(current->data < x)
            {
                if(current->rightChild == nullptr){
                    current = new node(x);
                    cout << current->data << endl;
                    break;
                }
                else{
                    current = current->rightChild;
                    root = current;
                }
            }
            else
            {
                break;
            }
        }
    }
    
}

int main(){

    tree t;
    t.insert(5);
    t.insert(10);
    t.print();

    return 0;
}

Here is another code that creates a temp pointer in a function that modifies the value. Although they are not the same data structures, but the idea is the same as my tree. The code below is verified and it is a good working code.

Second block of code:

void insertAfter(node* &hptr, int val, int searchValue){
  node* newNode = new node();
  node* temp = hptr; 
  newNode->data = val;
  while(temp->data != searchValue){
    temp = temp->next;
  }
  newNode->next = temp->next;
  temp->next = newNode; 
}

This is my first time writing a data structure in a class, so please go easy on me.

From my first code, when I have auto current = getRoot(); I am expect it is pointing to what ever the root is pointing, where the root is not a copy, it is a actual root.

For example:

void someFunction(node* root) -> this is simply a copy of the root.
void someFunction(node* &root) -> this is the ACTUAL root

In addition, if the root is pointing to root-> 1->2->3->4->5..., I am expecting the current is also pointing at:

root_____
         \
          1->2->3->4->5
         /
current--

So, when I am modifying the current pointer, I am expecting I am modifying the root because I am actually modifying the memory block.

Please help, and provide some good detailed explanation.

Aucun commentaire:

Enregistrer un commentaire