简体   繁体   English

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

[英]C++ Polynomial Class, pointer array issue

I have to write a program using this exact .h file not modifying anything. 我必须使用此确切的.h文件编写程序,而不修改任何内容。 It must overload a few operators and be able to add polynomials. 它必须重载一些运算符,并且能够添加多项式。 Here is the .h file. 这是.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

Here is the .cpp file. 这是.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;
  }
}

And so far this is my test.cpp file. 到目前为止,这是我的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;
}

The only error I'm getting using g++ is segmentation fault. 我使用g ++遇到的唯一错误是细分错误。 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* . 您永远不会为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. polyArray此处未初始化,但是您尝试访问其元素。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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