c++ - 多项式代码

标签 c++ function friend istream

我正在为我的 C++ 类(class)布置作业,但在运行该程序时遇到了一个小问题。我在调试时收到一条错误消息,指出 Pog11.exe 中 0x000944C8 处未处理的异常:0xC0000005:访问冲突写入位置 0x00000000.。目标是读入多项式的 int 度以及 double 系数。 这是我提供的 .h 文件:

#ifndef POLYNOMIAL_H
#define POLYNOMIAL_H

#include<iostream>
using std::ostream;
using std::istream;
using std::cerr;
using std::endl;

class Polynomial
{
    friend ostream& operator<<( ostream& left , const Polynomial&  right);
    friend istream& operator>>( istream& left , Polynomial& right );

public:
  Polynomial();
  Polynomial( int degree, const double* coefficients );
  Polynomial( const Polynomial& );
  ~Polynomial();

  const Polynomial& operator=( const Polynomial& deg);
  bool operator==( const Polynomial& deg ) const;
  void setDegree(int d);
  int getDegree() const;

private:
   int degree;
   double* coefficients;          
};
#endif 

下面是导致错误的代码段:

istream&  operator>>(istream& left, Polynomial& right)
{
    int tmp;
    left >> tmp;
    right.setDegree(tmp);
    int i = 0;
    while (i<=right.getDegree())
    {
        double co;
        left >> co;
        right.coefficients[i] = co;
        i++;
    }
    return left;
}

特别是 right.coefficients[i]=co; 行是导致程序崩溃的原因。

类的构造函数如下:

#include "Polynomial.h"
Polynomial::Polynomial() :degree(0), coefficients(0)
{
degree = 0;
coefficients = new double[degree];

}
Polynomial::Polynomial(int deg, const double* coefficients)
{
if (deg < 0)
{
    degree = 0;
}
else
{
    degree = deg;
}
coefficients = new double [degree];
}
Polynomial::Polynomial(const Polynomial& deg)
{
if (deg.getDegree() <= 0)
{
    setDegree(0);
}
else
{
    setDegree(deg.getDegree());
    for (int i = 0; i < degree; i++)
    {
        coefficients[i] = deg.coefficients[i];
    }
}
}

最佳答案

缺少代码 - 例如Polynomial 构造函数的实现对象。

我很确定您的错误是您没有为 coefficients 分配(足够)内存数据成员。必须有一个

coefficients = new double[number_of_coeffs]

代码中的某处。

编辑

您需要在多个点执行此操作:(重新)设置多项式的次数。因为这样你就知道多项式的次数了:

这里你必须复制传递的元素:

Polynomial( int degree, const double* coefficients ):
    coefficients( new double[degree] ), degree( degree )
{
    ::memcpy(this->coefficients, coefficients, degree * sizeof(double));
}

在这一项中,多项式的次数发生变化 - 因此您的系数数组必须相应地修改。

Polynomial::setDegree( int degree ) {
     // first delete the old coefficients
     delete [] coeffs;
     // and make a new array
     this->degree = degree;
     this->coefficients = new double[ this->degree ];
     // initialize the coefficients
     ..
 }

必须在复制构造函数和赋值运算符中完成类似的操作。

最后:您最好使用 std::vector<double>它基本上为你做了所有的内存管理。参见 http://en.cppreference.com/w/cpp/container/vector

关于c++ - 多项式代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29711458/

相关文章:

ruby - 我如何在 Ruby 中模拟类/方法的友谊?

c++ - 如何访问类成员

c++ - 优化代码 : is++p faster than p++?

c++ - 单向列表中的分段违规信号

c++ - const 访问和非常量访问

php - $(window).unload 中的killSession() PHP 函数

c++ - QString 中的哪个函数对用于转换为 std::string 或从 std::string 转换?

postgresql - Postgres 函数 : how to return the first full set of data that occurs after specified date/time

javascript - Typescript 中动态创建函数的形式参数

c++ - 应该使用哪个 "<<"运算符函数?