Jump to content
  • Advertisement
Sign in to follow this  
xomichelleybelly

why is this crashing...

This topic is 4261 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

void Inventory:: operator =(const Inventory &Inventory2) { ItemType *nodeptr; nodeptr=head; while (!IsEmpty()) { DropHeaviest(); } while ( nodeptr !=NULL) { // It gets to here fine after this it crashes PickUp(nodeptr->name, nodeptr->weight); nodeptr=nodeptr->next; } Heres the PickUp Function PickUp pre-condition: Item is Assigned post-condition: Item added to the inventory if not too heavy, && the message "xxx added to inventory" is printed otherwise, the message "xxx is to heavy to pick up" is displayed" ****************************************************************************************************************************/ void Inventory::PickUp(string itemname, int itemweight) {//start of function if (!IsTooHeavy(itemweight)) {//start of 1st if ItemType *newnode; ItemType *nodeptr; ItemType *previousnode=NULL; newnode= new ItemType; newnode->name=itemname; newnode->weight=itemweight; if (!head) {// start of 2nd if head=newnode; newnode->next=NULL; }// end of 2nd if else {//start of else nodeptr=head; previousnode=NULL; while (nodeptr!=NULL && nodeptr->weight > itemweight) {//start of while previousnode=nodeptr; nodeptr=nodeptr->next; }//end of while if (previousnode==NULL) {//start of 3rd if head= newnode; newnode->next=nodeptr; }//end of 3rd if else {//start of 2nd else previousnode->next = newnode; newnode->next=nodeptr; }//end of 2nd else }//end of 1st else cout << itemname <<" has been added to the list" << endl; currentweight= currentweight + itemweight; numitems++; }// end of 1st if else cout << "Sorry " << itemname << " is to heavy to add to the list" << endl; }// end of function

Share this post


Link to post
Share on other sites
Advertisement
Since I'm not sure how much you saw on IRC:

<MaulingMonkey> Guest4973: First you set nodeptr to head, then you drop <MaulingMonkey> In your operator=
<MaulingMonkey> See the problem?
<MaulingMonkey> Guest4973: I think you meant to do:
<MaulingMonkey> nodeptr = Inventory2.head;
<MaulingMonkey> Or similar
<MaulingMonkey> You should seriously consider using std::list<ItemType> instead of what you've got there.
<MaulingMonkey> Guest4973: Also, comments like "//start of 1st if" and "//start of 2nd if" are completely useless.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!