代码中没有错误,但是当我运行程序时,我得到“多项式.exe已停止工作”.错误



我有这个任务,我应该使用链表来存储多项式并对它们执行基本的数学函数。我编写了接受和打印多项式的代码。Eclipse没有显示任何错误,程序正常运行并接受系数,但之后它只是停止工作,并弹出一个对话框说"多项式。exe已停止工作"。下面是代码:

#include<iostream>
using namespace std;
    class term
    {
    private:
        int coef;
        int pow;
        term *next;
        friend class polynomial;
    public:
        term(int a,int b)
        {
            coef=a;
            pow=b;
            next=NULL;
        }
    };
    class polynomial
    {
    private:
        term *head;
    public:
        polynomial()
        {
            head=NULL;
        }
        void ini()
        {
            int deg;
            cout<<"Enter Degree of Polynomial";
            cin>>deg;
            head=create(deg);
        }
        term *create(int deg)
        {
            int coeff;
            cout<<"Enter Coefficient for the term x^"<<deg;
            cin>>coeff;
            term q=term(coeff,deg);
            if(deg==0)
            {
            q.next=NULL;
            }
            else
            {
            q.next=create(deg-1);
            }
            return &q;
        }
        void pri()
        {
            term *temp;
            for(temp=head;temp->next!=NULL;temp=temp->next)
            {
                cout<<temp->coef;
            }
        }
    };
    int main()
    {
        polynomial a=polynomial();
        a.ini();
        a.pri();
        return 0;
    }
谁能告诉我为什么会这样?

每次调用term *create(int deg)时,都获得一个悬空指针,因为它返回一个局部变量的地址。被寻址的局部变量在函数返回后立即消失。任何对返回指针的解引用都会调用未定义行为

下面是所讨论的成员函数的简化版本:

term *create(int deg)
{
  term q=term(coeff,deg); // local variable
  return &q;              // Oops!
}

参见局部变量的内存可以在其作用域之外访问吗?

通过将分配/存储与程序逻辑分离,可以极大地简化代码。例如,您可以使用std::vector在多项式类中存储项。您还应该将多项式类中系数值的读入和打印分离。例如,

class polinomial
{
  // construct polinomial of a certain order
  explicit polinomial(size_t order) : coeffs(order) {}
  // set coefficient of term of degree deg
  // return false if deg beyond order
  bool set_coeff(size_t deg, int coeff)
  {
    if (coeffs.size() < deg) return false;
    coeffs[deg] = coeff;
  }
  // obtain the coefficient of a term of degree deg
  int coeff(size_t deg) const { return coeffs[deg]; }
 private:
  std::vector<int> coeffs;
};

然后实现单独的函数或运算符来读取和打印系数

你可以尝试动态创建变量,就像这样:

class term
{
private:
   int coef;
   int pow;
   term *next;
   friend class polynomial;
public:
   term(int a,int b)
   {
      coef=a;
      pow=b;
      next=NULL;
   }
};
class polynomial
{
private:
   term* head;
public:
   polynomial()
   {
      head=NULL;
   }
   ~polynomial()
   {
      if(!!head)
      {
         delete head;
         head=NULL;
      }
   }
   void ini()
   {
      int deg;
      cout<<"Enter Degree of Polynomial";
      cin>>deg;
      head=create(deg);
   }
   term* create(int deg)
   {
      int coeff;
      cout<<"Enter Coefficient for the term x^"<<deg;
      cin>>coeff;
      term *q= new term(coeff,deg);
      if(deg==0)
      {
         q->next=NULL;
      }
      else
      {
         q->next=create(deg-1);
      }
      return q;
   }
   void pri()
   {
      term *temp;
      for(temp=head;temp->next!=NULL;temp=temp->next)
      {
         cout<<temp->coef;
      }
   }
};
int main()
{
   polynomial a=polynomial();
   a.ini();
   a.pri();
   return 0;
}

最新更新