Thread: Need help ASAP if you can

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    13

    Question Need help ASAP if you can

    I have a project due in 2 hours that I've been working on for weeks. I'm fairly confident that the project will work after I get help with this one function. I need to overload my assignment operator (=) so I can assign a returned Set to a newly created empty Set. I'm using linked lists and nodes containing one int value per node.

    Heres my deconstructor:

    Set::~Set()
    {
    while(head)
    {
    setNode *pTemp = head;
    head = head->next;
    delete pTemp;
    }
    }

    My overloaded = operator function is this:

    Set Set:perator = (const Set& S)
    {
    Set newSet(S);

    if(this != &S)
    ~this;

    for(setNode *p = S.head; p != NULL; p = p->next)
    newSet += p->setVal; //+= adds the value to the set

    return newSet;
    }


    I've tried it many different ways and although theres no syntax error, I keep getting memory overflow in my main before my while loop even reads once through. Maybe I'm not deconstructing something right? Any help is GREATLY appreciate. (Man had to type this post 3 times and 90% of the stuff in it, kept hitting escape after typing the code. Too used to vi in unix =(.
    Last edited by Wiggin; 12-05-2001 at 03:05 PM.

  2. #2
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    You don't want to call the destructor (otherwise the object would cease to exist). Just use the same code that you have in your destructor to delete each node. Also you'll want to return from the function immediately in the case of self-assignment.

    You don't need to create a new Set after (or before) deleting all the nodes. You can create a new node with the same value of S's head node, point your existing head node at this, and then copy all the other values across (creating new nodes) and attaching them.
    zen

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    13
    Set Set:perator = (const Set& S)const
    {


    if(this != &S)
    {
    while(head)
    {
    setNode *pTemp = head;
    head = head->next;
    delete pTemp;
    }
    }

    //here would go how to copy over whats on the right hand side of the = to the lh side. Can't I just return S and not have to copy anything?

    return newSet;
    }

    I see what your saying about calling the destructor. When I try to use the code that I have in my deconstrutor to delete each node it says "head = head->next" must be a modifiable lvalue. Not sure how to return to the function immediately in case of self-assignment. If I don't create a new Set i'm not sure how to use this->next or this->setVal to increment or if I could use *temp += m->setVal.

  4. #4
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    Can't I just return S and not have to copy anything?
    Then you haven't assigned anything to anything.

    I'm not sure how you are adding/inserting nodes, but you'd need to do something like -


    //Check for self assignment, if true
    //return

    //Delete all node's in set be assigned to

    //create new node
    Node* n = new Node;

    //obtain value from head node of
    //set to be copied. I don't know how you
    //obtain this value in your class
    n->value=S.GetHead()->value;

    //assign this->head to the newly created
    //node
    head=n;

    Node* temp = S.GetHead();
    while(temp->next != 0)
    {
    //Call function to add temp->next->value
    //to existing set.
    temp=temp->next;
    }

    return *this;
    zen

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need help ASAP ,
    By Haytham in forum C Programming
    Replies: 3
    Last Post: 05-14-2007, 10:21 AM
  2. Need Help ASAP....
    By Maxwell in forum C++ Programming
    Replies: 16
    Last Post: 09-14-2005, 06:56 PM
  3. Help Needed Please... Asap
    By Shabba in forum C Programming
    Replies: 2
    Last Post: 01-13-2004, 04:24 PM
  4. Count_nums (need help ASAP!)
    By legacye in forum C Programming
    Replies: 6
    Last Post: 11-22-2003, 06:32 PM
  5. Help Needed Asap With Searching An Array!
    By HelpMe++ in forum C++ Programming
    Replies: 5
    Last Post: 05-23-2003, 04:44 AM