C ++多项式类,指针数组问题

时间:2012-03-19 20:02:55

标签: c++ class pointers g++ operator-overloading

我必须使用这个确切的.h文件编写程序而不修改任何内容。它必须使一些运算符重载并能够添加多项式。 这是.h文件。

#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

这是.cpp文件。

#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;
  }
}

到目前为止,这是我的test.cpp文件。

#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;
}

我使用g ++获得的唯一错误是分段错误。我确定我搞砸了一些指针数组,但我不确定。可能存在一些问题。

1 个答案:

答案 0 :(得分:4)

您永远不会为int*分配内存。

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

polyArray未在此处初始化,但您尝试访问其元素。

首先为它分配内存:

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;
}