I am doing my school project, and I cannot copy the linked list correctly. When I assigned temp to this, nothing happens. I believe I put all the related code below, any help will be appreciated.

void poly::read ()
{
// Pre: None.
// Post: A new value is read into the implicit paramter polynomial,
// per instructions as given out first.  The terms are stored in
// decreasing order of exponents.  If necessary, the old value is destroyed.


poly temp;
char variable;
int coefficient;
int exponent;
cout << "Input a polynomial by first specifying the variable and then the terms in any order." << endl
<< "Each term is specified by an integer coefficient and" << endl
<< "a non-negative integer exponent." << endl
<< "Indicate END by specifying a dummy term with" << endl
<< "a zero coefficient and/or a negative exponent." << endl;
cin >> variable;
do
{
cin >> coefficient;
if (coefficient)
{
cin >> exponent;
if (exponent >= 0)
temp.InsertTerm (term(variable, coefficient, exponent));
}
else
while (cin && (cin.peek() != '\n'))
cin. ignore();
} while (coefficient && (exponent >= 0));
*this = temp; // The assignment operator is being called here!
};
Assignment operator
poly & poly::operator= (const poly & p) //DEEP COPY SEMANTICS
{
// Pre: p is a valid polynomial.
// Post: The value of p is assigned to the implicit parameter
// by "deep copy semantics."  Any necessary deallocation is
// done along the way.


if (this != &p)
{
free ();
copy (p);
};
return (*this);
};


void poly::free (void)
{
// Pre: The implicit parameter has been allocated.
// Post: Any necessary deallocation is done.


list_clear(terms);
};


void poly::copy (const poly & p)
{
// Pre: p is a valid polynomial.
// Post: p is copied into the implicit parameter.


Node* head_ptr;
Node* tail_ptr;
list_copy(p.terms, head_ptr, tail_ptr);
};


void list_clear(Node*& head_ptr)
// Library facilities used: stdlib.h
{
while (head_ptr != NULL)
list_head_remove(head_ptr);
}


void list_copy(Node* source_ptr, Node*& head_ptr, Node*& tail_ptr)
// Library facilities used: stdlib.h
{
head_ptr = NULL;
tail_ptr = NULL;


// Handle the case of the empty list
if (source_ptr == NULL)
return;


// Make the head node for the newly created list, and put data in it
list_head_insert(head_ptr, source_ptr->data);
tail_ptr = head_ptr;


// Copy the rest of the nodes one at a time, adding at the tail of new list
for (source_ptr = source_ptr->link; source_ptr != NULL; source_ptr = source_ptr->link)
{
list_insert(tail_ptr, source_ptr->data);
tail_ptr = tail_ptr->link;
}
}



void list_head_insert(Node*& head_ptr, const Node::Item& entry)
{
Node *insert_ptr;


insert_ptr = new Node;
insert_ptr->data = entry;
insert_ptr->link = head_ptr;
head_ptr = insert_ptr;
}


void list_insert(Node* previous_ptr, const Node::Item& entry)
{
Node *insert_ptr;


insert_ptr = new Node;
insert_ptr->data = entry;
insert_ptr->link = previous_ptr->link;
previous_ptr->link = insert_ptr;
}

Edited 3 Years Ago by happygeek: fixed formatting

Do you mean change
poly & poly::operator= (const poly & p)
to
poly poly :: operator = (const poly & p)

Forget it. That probably won't help. It's best to post a compilable / runnable single-file piece of code if possible.

It's not too big project but it does has couple of files in it, and Im not quite sure how to combine them together.

I use the debugger, and the problem seems in this piece of code:

poly &poly::operator= (const poly & p) //DEEP COPY SEMANTICS
{
// Pre: p is a valid polynomial.
// Post: The value of p is assigned to the implicit parameter
// by "deep copy semantics."  Any necessary deallocation is
// done along the way.


if (this != &p)
{
free ();
copy (p);
};
return (*this);
};

it seems run just find in the copy method, however, it doesn't appear to change the implicit parameter at all.

Edited 3 Years Ago by happygeek: fixed formatting

Forget it. That probably won't help. It's best to post a compilable / runnable single-file piece of code if possible.

Never mind, I fixed it, thanks though!

This article has been dead for over six months. Start a new discussion instead.