代码中没有错误,但是当我运行程序时,我得到了#34; Polynomial.exe已停止工作"错误

时间:2014-08-15 08:29:11

标签: c++

我有这个任务,我应该使用链表来存储多项式并对它们执行基本的数学函数。 我编写了接受和打印多项式的代码。 Eclipse没有显示任何错误,程序运行并正确接受系数,但之后它只是停止工作,弹出一个对话框说" polynomial.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;
    }

有人能告诉我为什么会这样吗?

2 个答案:

答案 0 :(得分:1)

每次调用term *create(int deg)时,都会获得一个悬空指针,因为它返回一个局部变量的地址。函数返回后,本地变量地址不再存在。对返回指针的任何取消引用都会调用 undefined behviour

以下是有问题的成员函数的精简版本:

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

请参阅Can a local variable's memory be accessed outside its scope?

您可以通过从程序逻辑中解除分配/存储来大大简化您的代码。例如,您可以使用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;
};

然后实现单独的函数或运算符以读入和打印系数。

答案 1 :(得分:0)

你可以尝试动态创建变量,如下所示:

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