2

I am writing a program to implement a linked list. The list contains nodes that are used to represent terms in a polynomial function. I have a class called Polynomial where I instantiate the linked list. The class has methods that allow you to modify the polynomial, including an addTerm method.

In my main, I would like to do something like this:

Polynomial p, q;
p.addTerm({ 4, 8 });
p.addTerm({ -3, 2 });
q = p;
p.addTerm({ 5, 6 });
p.addTerm({ 3, 2 });

this is the full Polynomial class:

class Polynomial {
        Node* Head;
    public:
        Polynomial();
        ~Polynomial();
        void addTerm(Term term);
        unsigned int degree() const;
        double coefficientFor(unsigned int exponent) const;
        void clear();

    private:
        void DeleteInvalidNode();
    };

and the constuctor:

Polynomial::Polynomial()
    {
        Head = 0;
    }

For some reason, though, when I add the terms to p after copying p to q it adds those terms to q. From what I understand this is because when you copy an object to another object they point to the same thing. Is there any way that I can fix this to where when I modify p it doesnt effect q

EDIT: I have now made an operator copy assignment function to try and copy the linked list.

I have written the following:

Polynomial& Polynomial::operator=(const Polynomial& rhs)
    {
        Node* Temp = rhs.Head;

        while (rhs.Head != NULL)
        {
            Temp = rhs.Head->next;
            this->addTerm({ rhs.coefficientFor(degree()), rhs.degree() });
            rhs.Head = Temp;
        }

        return *this;
    }

It is not working though since rhs is constant and I am trying to modify it. I am very new to linked lists. How would I go about fixing this to where it can work?

  • 2
    You probably want to do a [deep copy](https://stackoverflow.com/questions/184710/what-is-the-difference-between-a-deep-copy-and-a-shallow-copy). – IWonderWhatThisAPIDoes Mar 01 '21 at 07:34
  • 6
    Maybe you didn't properly implement the [rule of 3/5/0](https://en.cppreference.com/w/cpp/language/rule_of_three). Please show a [mre] if you want a deeper diagnosis. – Lukas-T Mar 01 '21 at 07:37

1 Answers1

2

This is likely due to how you define the "=" operator in the Polynomial class. In here, you have to make sure that it's creating new copies of the terms rather than pointing to the contents of the other Polynomial object.

Make the following function a public function in Polynomial:

Polynomial& operator=(const Polynomial& rhs){
      /*Not sure the specifics of your functions or the intention of Nodes, so
      if you need to iterate throuch each term in a Polynomial, be sure to do so, 
      and put the statement below within your loop.*/

    this->addTerm({rhs->coefficientFor(rhs->degree()),rhs->degree()}); 

       /*or something to that effect, what matters is being able to retrieve the
       each Term in the rhs (right-hand-side) and adding them to this 
       Polynomial.*/
    return this*;
Wes
  • 104
  • 5
  • How would I go about doing this? I edited my question and added the class and the contructor – ParkerHarrelson123 Mar 01 '21 at 07:59
  • @ParkerHarrelson123 I put the code in my answer! Take it as a strong guideline, and edit it to fit your specific needs! – Wes Mar 01 '21 at 08:15
  • When I put this code snippet in, it gives me an error saying 'operator' must be a member function. And then 'this' may only be used inside a nonstatic member function – ParkerHarrelson123 Mar 01 '21 at 14:32
  • I'm pretty sure that all you need to do is add ```Polynomial::``` before ```opeartor=``` in the function header. Hope this helps! – Wes Mar 01 '21 at 17:42
  • that was my bad, I forgot to put that in my header file. I seem to be having a hard time iterating through the linked list in the function (I am very new to linked lists). I am adding my attempt at iterating through it to my original post. It doesn't seem to work. Is there any help you could give me with it? – ParkerHarrelson123 Mar 01 '21 at 20:25
  • You don't want to modify ```rhs```. ```rhs``` stands for right-hand-side of the "=" operator. In your ``main```, ```p``` is the ```rhs```. You want to modify ```this```, which in your situation is ```q```. Inside ```operator=()```, you want to take the values (not addresses) of q and create variables that take those in. Then ```AddTerm()``` those variables with ```this```. – Wes Mar 02 '21 at 01:00