简体   繁体   English

C++ class:必须显式初始化成员

[英]C++ class: must explicitly initialize the member

So, I have made this class called 'Grid'.所以,我把这个 class 称为“网格”。 I also have a class called 'Field'.我还有一个名为“Field”的 class。 The Field-class has 4 variables of the type Grid and they are all set in the constructor. Field 类有 4 个 Grid 类型的变量,它们都在构造函数中设置。 except, they're not... Or at least the compiler doesn't think so.除了,他们不是......或者至少编译器不这么认为。

I don't really get why, I set all the variable to a new instance.我真的不明白为什么,我将所有变量设置为一个新实例。

When I hover over the red underline (under the constructor) it says: "constructor for 'Field' must explicitly initialize the member 'b_attack' which does not have a default constructor"当我在红色下划线(在构造函数下)上的 hover 上显示:“'Field' 的构造函数必须显式初始化没有默认构造函数的成员 'b_attack'”

code:代码:

class Grid{
    friend class Field;
private:
    unsigned int xSize;
    unsigned int ySize;
    bool *ptrfield;
    Grid(unsigned int xSize, unsigned int ySize){
        this->xSize = xSize;
        this->ySize = ySize;
        ptrfield = new bool[xSize*ySize];
    }
};

class Field{
private:
    Grid a_own;
    Grid a_attack;
    Grid b_own;
    Grid b_attack;
public:
    Field(unsigned int xSize, unsigned int ySize){
        a_own = Grid(xSize, ySize);
        a_attack = Grid(xSize, ySize);
        b_own = Grid(xSize, ySize);
        b_attack = Grid(xSize, ySize);
    }
    void print(){
        std::cout << "aHELLO" << std::flush;
        std::cout << a_own.xSize << std::flush;
    }
}; 

The problem is that your 4 Grid member objects are created before the body of your Field constructor is entered (or at least, the compiler wants to generate code to create them - but it can't);问题是在输入Field构造函数的主体之前创建了 4 个Grid成员对象(或者至少,编译器想要生成代码来创建它们 - 但它不能); see this cppreference (bolding mine):看到这个cppreference (加粗我的):

Before the compound statement that forms the function body of the constructor begins executing , initialization of all direct bases, virtual bases, and non-static data members is finished .在构造函数的 forms 和 function 主体的复合语句开始执行之前,所有直接基、虚拟基和非静态数据成员的初始化已完成 Member initializer list is the place where non-default initialization of these objects can be specified.成员初始化器列表是可以指定这些对象的非默认初始化的地方。 For members that cannot be default-initialized , such as members of reference and const-qualified types, member initializers must be specified.对于不能默认初始化的成员,例如引用和 const 限定类型的成员,必须指定成员初始化器。 No initialization is performed for anonymous unions or variant members that do not have a member initializer.对于没有成员初始化程序的匿名联合或变体成员不执行初始化。

From this quote (the remarks about "members that cannot be default-initialized"), we then see that we can provide the required initialization in an initializer list;从这个引用(关于“不能默认初始化的成员”的注释),我们可以看到我们可以在初始化列表中提供所需的初始化; this is placed immediately after the closing ) of the constructor declaration, using a : followed by the required, comma-separated initializers. this 放置在构造函数声明的)结束之后,使用:后跟所需的逗号分隔的初始值设定项。 Like this, in your case:像这样,在你的情况下:

    Field(unsigned int xSize, unsigned int ySize) : // The ":" starts our initializer list
        a_own(xSize, ySize),      // .. and each one of these constructs a "Grid"
        a_attack(xSize, ySize),
        b_own{ xSize, ySize },    // This "{...}" form is newer and many prefer it,
        b_attack{ xSize, ySize }  // but it can also be a bit confusing
    {
        return; // We now have nothing left to do in the actual "body" of the c'tor!
    }

Please feel free to ask for further clarification and/or explanation.请随时要求进一步澄清和/或解释。

PS: For your posted code, my compiler gives the error you reported for all four Grid members; PS:对于您发布的代码,我的编译器给出了您为所有四个Grid成员报告的错误; perhaps yours 'gives up' after the first one?也许你的“放弃”在第一个之后?

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

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