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!