简体   繁体   English

使用默认类构造函数C ++遇到麻烦

[英]Having trouble with default class constructor C++

I am having some trouble understanding default constructor methods in C++ and how to write one to fit the needs of my methods. 我在理解C ++中的默认构造函数方法以及如何编写满足我的方法需求的方法时遇到了一些麻烦。 I was tasked with creating a simple array that could represent polynomials, such as a(n)x^(n) + a(n-1)x^(n-1) + … + a(0). 我的任务是创建一个可以表示多项式的简单数组,例如a(n)x ^(n)+ a(n-1)x ^(n-1)+…+ a(0)。 It should be array of coefficients, with coefficient a(i) being stored in location i of the array. 它应该是系数数组,系数a(i)存储在数组的位置i中。 The coefficients are floating point values (potentially negative), so the array should be of type double. 系数是浮点值(可能为负),因此数组应为double类型。 It should also be of size MAXPOLY (a constant variable set to 50), limiting the array to holding polynomials with maximum degree of MAXPOLY – 1 (or 49). 它的大小也应为MAXPOLY(将常量设置为50),从而将数组限制为保留最大程度为MAXPOLY – 1(或49)的多项式。
The full assignment instructions/description can be found here: Beginner polynomial program in C++ 完整的分配说明/描述可以在这里找到: C ++中的初学者多项式程序

My issue lies in creating the default constructor for the polynomial object, Poly. 我的问题在于为多项式对象Poly创建默认构造函数。 The default class constructor is meant to initialize a polynomial to the constant 0, creating a class object that is a degree-0 polynomial of 0. The assignment also notes, however, that a post condition of this constructor is that all array elements of coeff[] are set to 0.0 and that is where my confusion ensues. 默认的类构造函数用于将多项式初始化为常数0,创建一个类对象,该类对象是0的0级多项式。但是,赋值还指出,该构造函数的后置条件是coeff的所有数组元素[]设置为0.0,这是我感到困惑的地方。 Should first initialize the entire array for 0-MAXPOLY and then define the poly at 0? 应该首先为0-MAXPOLY初始化整个数组,然后在0处定义poly吗? Or should one or the other be sufficient? 还是一个就足够了? How should I go about writing the constructor? 我应该如何编写构造函数?

I apologize if this question seems rather obvious, but I am brand new to C++ and the textbook resources I have read on this subject have been unclear. 如果这个问题看起来很明显,我深表歉意,但是我是C ++的新手,关于这个主题的教科书资源尚不清楚。 Also, I think I may be overthinking and confusing myself. 另外,我想我可能会想得太多,让自己感到困惑。 Any help would be greatly appreciated! 任何帮助将不胜感激! Thank You! 谢谢!

I do believe you are overthinking the problem. 我相信您是在想这个问题。 Just as a polynomial with coeff[0] = 10 is "x = 10", one with coeff[0] = 0 is "x = 0", assuming the rest of the array is initialized to zero. 正如coeff [0] = 10的多项式是“ x = 10”,coeff [0] = 0的多项式是“ x = 0”,假定数组的其余部分都初始化为零。

As a beginner in C++, keep in mind that array indexes are from 0 to N-1 where N is the size of the array. 作为C ++的初学者,请记住,数组索引的范围是0到N-1,其中N是数组的大小。

You should store the coefficients in an array. 您应该将系数存储在数组中。 You have two general options: 您有两种常规选择:

  • Use a fixed-size array and also a counter of how many elements of the array are "in use" 使用固定大小的数组以及计数器“正在使用”多少个元素的计数器
  • Use a variable-sized array 使用可变大小的数组

The second option is simpler and makes more sense, so your class would have a member 第二个选项更简单,更有意义,因此您的班级将有一个成员

std::vector<double> coefficients;

and in the constructor initializer list you would have: 在构造函数初始化器列表中,您将拥有:

coefficients(1, 0.0)

meaning to initialize it to having 1 member of value 0.0 . 意味着将其初始化为具有1个值为0.0成员。 Then you can find the degree of the polynomial by doing coefficients.size() - 1 . 然后,您可以通过执行coefficients.size() - 1来找到多项式的次数。

Looking at the linked question, it appears you do not have a choice about what representation to use. 查看链接的问题,似乎您无法选择要使用哪种表示形式。 Assuming this: 假设这样:

class Poly
{...
    // array for holding the coefficients of the poly
    double coeff[MAXPOLY];               
...
}

Then, your constructor needs to fill the array with zero values, like this: 然后,构造函数需要使用零值填充数组,如下所示:

// Class constructor
Poly::Poly ()
{ 
   //ADD YOUR CODE HERE
   for( int i = 0; i < MAXPOLY; i++ ) 
       coeff[i]=0.0;
}

This is the most explicit demonstration of what's going on, though you can shorten the code to initialize an array to something like this: 这是正在发生的事情的最明确的说明,尽管您可以缩短将数组初始化为类似以下代码的代码:

memset(coeff, 0, sizeof(coeff)); memset(coeff,0,sizeof(coeff)); // this fills every byte of the array's memory space with zero. //这将用零填充数组存储空间的每个字节。

functionally they produce the same thing. 在功能上,它们产生相同的东西。

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

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