【问题标题】:C++ Polynomial Class, pointer array issueC++多项式类,指针数组问题
【发布时间】:2012-03-19 20:02:55
【问题描述】:

我必须使用这个确切的 .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++ 时遇到的唯一错误是分段错误。我确定我搞砸了一些指针数组,但我不确定。可能有一些问题。

【问题讨论】:

  • 投票结束:让陌生人通过检查发现代码中的错误是没有效率的。您应该使用调试器或打印语句来识别(或至少隔离)问题,然后返回更具体的问题(一旦您将其缩小到 10 行 test-case)。

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


【解决方案1】:

你永远不会为你的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;
}

【讨论】:

  • polyArray 应该是std::vector&lt;int&gt; polyVector
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-12-16
  • 2011-05-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多