简体   繁体   中英

C++ Polynomial Class, pointer array issue

I have to write a program using this exact .h file not modifying anything. It must overload a few operators and be able to add polynomials. Here is the .h file.

#ifndef H_polyClass
#define H_polyClass

#include <iostream>
using namespace std;

class polyClass
{

        friend ostream& operator << (ostream&, const polyClass&);
        //Overload the extraction operator
        //Output format example:  5x^2 - 3x + 6

public:
        void setPoly ( int myArray [], int myterms);
        //Function to set the polynomial according to the parameter.
        //Postcondition: polyArray = myArray; terms = myterms;

        polyClass ( int myArray [], int myterms);
        //Constructor
        //Initializes the polynomial according to the parameter.
        //Postcondition: polyArray = myArray; terms = myterms;

        polyClass ();
        //Default Constructor
        //Initializes the polynomial.
        //Postcondition: polyArray = NULL; terms = 0;

        polyClass operator+ (const polyClass& otherPoly) const;
        //overload the operator +

        bool operator == (const polyClass& otherPoly) const;
        //overload the operator ==

private:
        int *polyArray;         //pointer to an array that holds the term coefficients.
        int terms;                      //the number of terms in the polynomial

};

#endif

Here is the .cpp file.

#include <iostream>
#include "polyClass.h"
using namespace std;
ostream &operator<<(ostream &out, const polyClass &poly)
{
  out << poly.polyArray[1];
  if(poly.terms > 1)
    out << poly.polyArray[2];
  if(poly.terms > 2){
    for(int i = 2; i < poly.terms; i++){
      out << poly.polyArray[i] << "x^" << i;
    }
  }
  return out;
}
void polyClass::setPoly(int myArray[], int myterms)
{
  for(int i = 0; i < myterms; i++)
    polyArray[i] = myArray[i];
  terms = myterms;
}
polyClass::polyClass(int myArray[], int myterms)
{
  for(int i = 0; i < myterms; i++)
    polyArray[i] = myArray[i];
  terms = myterms;
}
polyClass::polyClass()
{
  polyArray = NULL;
  terms = 0;
}
polyClass polyClass::operator+(const polyClass &otherPoly) const
{
  int size;
  if(this->terms >= otherPoly.terms)
    size = this->terms;
  else
    size = otherPoly.terms;
  int *newArray = new int[size];
  for(int i = 0; i < size; i++)
    newArray[i] = otherPoly.polyArray[i] + this->polyArray[i];
  return polyClass(newArray, size);
}
bool polyClass::operator==(const polyClass &otherPoly) const
{
  if(otherPoly.terms != this->terms){
    return false;
  }
  else if(otherPoly.terms == this->terms){
    for(int i = 0; i < otherPoly.terms; i++){
      if(polyArray[i] == this->polyArray[i])
        return true;
    }
  }
  else{
    return false;
  }
}

And so far this is my test.cpp file.

#include <iostream>
#include "polyClass.h"
using namespace std;

int main()
{
  int myarray[] = {2,4,5};
  int myarray2[] = {2,4,5};
  int myarray3[] = {2,4};
  polyClass p1(myarray, 3);
  polyClass p2(myarray2, 3);
  polyClass p3(myarray3, 2);
  cout << "PolyClass p1 is : " << p1 << endl;

  return 0;
}

The only error I'm getting using g++ is segmentation fault. I'm sure I have messed up some pointer array but I'm not sure. There is probably a few issues.

You never allocate memory for your int* .

polyClass::polyClass(int myArray[], int myterms)
{
  for(int i = 0; i < myterms; i++)
    polyArray[i] = myArray[i];
  terms = myterms;
}

polyArray is not initialized here, but you try to access its elements.

Allocate memory for it first:

polyClass::polyClass(int myArray[], int myterms)
{
  polyArray = new int[myterms];  //allocate memory here
  for(int i = 0; i < myterms; i++)
    polyArray[i] = myArray[i];
  terms = myterms;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM