小编Jul*_*sen的帖子

初始化类中结构的值

我在基本Polynomial类.h文件的私有部分中有以下代码:

private:
struct Term
{
    float coefficient;
    int power; // power of specific term
};
int degree; //total number of terms
Run Code Online (Sandbox Code Playgroud)

我有Polynomial类的以下默认构造函数:

Polynomial::Polynomial()
{
    Polynomial.Term poly;
    poly.power = 0;
    poly.coefficient = 0;
    degree = 1;
}
Run Code Online (Sandbox Code Playgroud)

我对如何访问结构中的术语以及结构外部的变量感到困惑.我试图谷歌这个,但找不到任何有用的东西.

编辑:重载输出操作员代码

ostream & operator << (ostream & outs, Polynomial & P)
{
    outs << P[0].poly.coefficient << "x^" << P[0].poly.power;
    for (int i=1; i<P.degree-1; i++)
    {
        if (P[i].poly.coefficient > 0)
            outs << "+";
        outs << P[i].poly.coefficient << "x^" << P[i].poly.power;
    }
    outs << endl; …
Run Code Online (Sandbox Code Playgroud)

c++ struct class

3
推荐指数
1
解决办法
44
查看次数

标签 统计

c++ ×1

class ×1

struct ×1