C_Rod
C_Rod

Reputation: 303

Private pointer in class doesn't update when changed in method

I have these classes (stripped for readability)

class node {
   public:
   int x;
   node* next;

   node(){}
   ~node(){}
 };

 class intLinkedList{
     public:
         intLinkedList();
         ~intLinkedList();

         void Add (int newX);

     private:
         node* root;
  };

This is the implementation in Add

void intLinkedList::Add (int newX){
  node* newNode = new node();
  newNode->x = newX;
  newNode->next = NULL;

  std::cout << "\n\n" << root << "\n\n" << std::flush;

  if (root == NULL){
    root = newNode;    
    return;
  }

  node * current;
  current = root;

  while (current->next != NULL){
    current = current->next;
  }

  current->next = newNode;

  return;
}

When I printout the address pointed to by root immediately after setting it, it shows a valid address. However, the next time I call Add, root has become NULL again. I cannot imagine what behavior is causing this. This is used absolutely nowhere else.

I fully realize that there is something simple I'm missing. If your inclination is to down vote because the problem is simple, take it elsewhere. The purpose of this platform is for coders to come together to help each other when we have coding brainfarts.

EDIT: This is the driver.

#include <string>
#include <iostream>
#include "intLinkedList.h"

using namespace std;

void AddValue(intLinkedList MyList);
void GetValue(intLinkedList MyList);
void InsertValue(intLinkedList MyList);
void DeleteValue(intLinkedList MyList);
void PrintList(intLinkedList MyList);

int main(){
    intLinkedList MyList;
    int Option;

    while (true){

        cout << "\n\nMain Menu\n---------\n\n1) Add Value\n2) See Value\n3)     Insert Value at Position\n4) Delete Value at Position\n5) Print List\n6)     Exit\n\n";
    cin >> Option;

        switch (Option){
            case 1: AddValue(MyList); break;
            case 2: GetValue(MyList); break;
            case 3: InsertValue(MyList); break;
            case 4: DeleteValue(MyList); break;
            case 5: PrintList(MyList); break;
            case 6: exit(0);
        }
    }
}

void AddValue(intLinkedList MyList){
    int NewValue;
    cout << "What value should be added?\n";
    cin >> NewValue;

    MyList.Add(NewValue);
}

void GetValue(intLinkedList MyList){
    int Position;
    cout << "What position do you want the value of?\n";
    cin >> Position;

    MyList.Get(Position);
}

void InsertValue(intLinkedList MyList){
    int Position;
    int NewValue;
    cout << "What position do you wnat to insert after?\n";
    cin >> Position;
    cout << "\nWhat value do you want to insert?\n";
    cin >> NewValue;

    MyList.Insert(NewValue, Position);
}

void DeleteValue(intLinkedList MyList){
    int Position;
    cout << "What position do you want to delete?\n";
    cin >> Position;

    MyList.Delete(Position);
}

void PrintList(intLinkedList MyList){
    cout << MyList.Print();
}

Upvotes: 0

Views: 127

Answers (1)

Klaus
Klaus

Reputation: 25623

BTW: I wonder why people write linked list implementations? Why not using the c++ standard library?

void AddValue(intLinkedList MyList);

This generates a complete new MyList item. You should use a reference instead!

void AddValue(intLinkedList& MyList);

EDIT:

Why you use

 case 1: AddValue(MyList); break;

instead of:

 MyList.Add(...);

Any kind of indirection increase the risk of mistakes, complexity and unreadability. Your question is a very good example!

That is the first I see. Maybe there is a lot more.

Hope this is an entry point.

Upvotes: 6

Related Questions