简体   繁体   中英

C++ class: must explicitly initialize the member

So, I have made this class called 'Grid'. I also have a class called 'Field'. The Field-class has 4 variables of the type Grid and they are all set in the constructor. 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"

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); see this cppreference (bolding mine):

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 . 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. 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. 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; perhaps yours 'gives up' after the first one?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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