FranciscoS
FranciscoS

Reputation: 11

C++ - Array values in class member function changing spontaneously

I'm having some issues overloading the *= operator in my polynomial class. I've included all I believed relevant to solving the problem below. I apologize for the length of the code.

Here are the parts of my class header I think are relevant.

#ifndef POL_H
#define POL_H
#include <string>
#include <iostream>
#include <vector>
#include <cmath>

using namespace std;

class Pol
{
  // private data members

  string polname;  // polynomial name
  int degree;      // polynomial degree
  int *coef;       // array with polynomial coefficients

 public:

  //  constructors

  Pol(string, int, int*);         // with input name, degree 
                                  // and array of coefficients
  // operator overloading

  Pol operator *= (Pol);

  //  other methods

  void PrintPol ();

Here is my .C file with the overload of *=. I decided not to include the PrintPol method as it is very long and I'm almost 100% sure the problem does not lie with it.

#include "Pol.h"

Pol::Pol (string s, int d, int *c)
{
  degree = d;
  polname = s;
  coef = new int [degree + 1];
  coef = c;

  // initializes polynomial of degree d with c coefficients
  // named s
}

Pol Pol::operator *= (Pol p1)
{
  int *cp = this->coef; // If I print the values stored in cp at this point 
                        // everything is alright

  for (int i = p1.degree; i >= 0; --i)
    {
      for (int j = this->degree; j >= 0; --j)
    {
      this->coef[i + j] += p1.coef[i] * cp[j];
      cout << cp[j] << endl; // When I print the values here, they've changed!
    }
    }

  this->degree += p1.degree;

  return *this;
}

My first thought was that maybe I was overstepping the bounds of the array, but the size of the array cp I create is this->degree, and that is also the highest value that "j" assumes, so I think that can't be it.

Here is just my main function. I doubt the problem is here, but I included it anyway, so you could see how I'm using the methods I've declared.

#include "Pol.h"
#include <iostream>

using namespace std;

int main ()
{
  int a [9] = {0, 0, 2, 4, 0, 0, 5, 0, 1};
  int b [5] = {4, -2, 0, 0, 1};

  Pol P2 ("P2", 4, b);
  Pol P4 ("P4", 8, a);

  P4*= P2;
  P4.PrintPol();

  return 0;
}

This is probably really obvious and I'm just making an ass out of myself, but I've staring at the code for hours and I can't figure it out. Thanks in advance.

Upvotes: 0

Views: 402

Answers (1)

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385114

You're asking about overloading *=, yet the problem has nothing to do with *=. You say your array values are changing "spontaneously", yet you're clearly changing them yourself, right there in the code.

int *cp = this->coef; // "If I print the values stored in cp at this point 
                      // everything is alright"

for (int i = p1.degree; i >= 0; --i) {
   for (int j = this->degree; j >= 0; --j) {
      this->coef[i + j] += p1.coef[i] * cp[j]; // <-- Well, you changed them here mate
      cout << cp[j] << endl; // "When I print the values here, they've changed!"
   }
}

From the way you commented coef's declaration:

int *coef;       // array with polynomial coefficients

it seems like you think coef is an array; it's not. You also seem to think that assigning coef to another int* will copy the underlying data that it points to; it will not.

coef is a pointer, and assigning it to another pointer simply copies the pointer.

Try using a std::vector like everybody else so that data ownership and lifetime is managed for you, and so that you may take advantage of the basic assignment operations.

Upvotes: 1

Related Questions