trying to set data for a node class, not working?

This is a discussion on trying to set data for a node class, not working? within the C++ Programming forums, part of the General Programming Boards category; Okay I have an object called Resident and it has fields for name and bill. In my program I am ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    29

    trying to set data for a node class, not working?

    Okay I have an object called Resident and it has fields for name and bill. In my program I am going thru a linked list of dirty laundry items and when i get each item I am trying to adjust the owner of that items bill accordingly. The problem is for some reason when using my node pointer the setBill() function appears to do nothing! Here is some code to clarify -

    node class:
    Code:
    template <class T>
    class node{
    
      public:
             node(){linkfield = NULL;}
             node(T d, node *lf){datafield = d; linkfield = lf;}
      
      T data(){return datafield;}
      node * link(){return linkfield;}
    
      void setData(T d){datafield = d;}
      void setLink(node * lf){linkfield = lf;}
       
     private: 
               T datafield;
                node * linkfield;
    };
    resident:
    Code:
    class Resident{
    
      public:
             Resident();
             Resident(std::string n, double b);
      
      std::string getName() {return name;}
      double getBill() {return bill;}
    
      void setName(std::string n){name = n;}
      void setBill(double b){bill = b;}
      
      friend std::ostream& operator <<(std::ostream& outs, Resident o);
      friend bool operator ==(const Resident& r1, const Resident& r2);	
      friend bool operator !=(const Resident& r1, const Resident& r2);
    
     private: 
               std::string name;
               double bill;
    };
    And the code where it is all happening:
    Code:
    void Laundry::doLaundry(){
         if(dirty < 7)
          cout << "\nThere are not enough dirty clothes to make a full load.\n";
       else
       {
          node <Clothing> * mover = d_head; //sets the mover pointer to the top of the dirty clothes linked list.
          while(mover != NULL)
          {
             Clothing ctmp = mover->data();
             addClean(ctmp); //adds clothes to clean clothes linked list (works)
             removeDirty(ctmp); //removes clothing item from dirty linked list (works)
             mover = mover->link(); //move to next item
             node <Resident> * ptr = r_head; //set ptr to top of resident linked list
             while(ptr != NULL)
             {
             if(ptr->data().getName() == ctmp.getOwner().getName()){ //if the name of the resident matches the name of the clothing's owner
                cout << "This is ptr: " << ptr->data();     //output the resident's info (for debugging)            
                ptr->data().setBill(5.5); //this should easily change the bill of that resident (DOES NOT WORK???)
                cout << "This is ptr with new bill: " << ptr->data(); //should output the same resident with new bill but gives same output as above
             }
             ptr = ptr->link(); //move to next resident
             }
          }
       }
    }
    I tried to comment as much as possible so you can get an idea of what my code is doing. I just do not understand why setBill() wouldnt work, it is one line of friggin code that is amazingly simple yet doesn work. Any help would be really great, I'm nearly at the end of my rope.
    Thanks!

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Yup. Thats what your code will do. In fact SetBill is doing what it should. The key to this is what is it doing it on. The answer is a COPY of your object and not your object itself. The offending function is Data(). See if you can work out the fix without help.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    29
    Alright I got it, I had never returned something by reference but I figured it out, thanks

  4. #4
    Registered User
    Join Date
    Apr 2005
    Posts
    29

    More linked list trouble

    I'm having some more trouble with this linked list stuff... my new problem is that my removeDirty() and removeClean() functions only remove nodes that are at the top of the list, not in the middle. I basically took this code straight from my notes and cant really find anything similar (using a node class) in the book. Here it is:

    Code:
    void Laundry::removeClean(Clothing c){
       node <Clothing> * tmp;
       if(c_head == NULL) return;
       if(c_head->data() == c)
       {
          tmp = c_head;
          c_head = c_head->link();
          delete tmp;
       }
       else
       {
          tmp = c_head->link();
          node <Clothing> * previous = c_head;
          while(tmp != NULL && tmp->data() != c){
             tmp = tmp->link();
             previous = previous->link();
          }
          if(tmp == NULL)
             cout << "That item was not found in the list.\n";
          else if(tmp->data() == c)
          {
             previous->setLink(tmp->link());
             delete tmp;
          }
       }
       clean--;
    }
    thanks again

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    If you can successfully remove the first and the last nodes of the list, then you can probably write code remove an interior node as well, and just don't know it yet. I'd suggest you use a pencil and paper. Represent a node as a letter representing the data and an arrow representing the pointer (use multiple arrows if the node has multiple pointers). Now create a list of three nodes, A->B->C->null. Now which pointer will you need to reassign to which node to remove B, which is an interior node? How will you know that your current node is B? How can you tell what the next node after B is and what the prior node before B is? (Hint, you can have more than one pointer pointing to nodes in the list at any given time).
    You're only born perfect.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. compiler build error
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-30-2003, 09:16 AM
  4. can't insert data into my B-Tree class structure
    By daluu in forum C++ Programming
    Replies: 0
    Last Post: 12-05-2002, 05:03 PM
  5. List class
    By SilasP in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2002, 04:20 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21