2021-11-08 error: could not convert ‘{0}‘ from ‘<brace-enclosed initializer list>‘ to

could not convert ‘{0}’ from ‘《brace-enclosed initializer list>’ to

C + + program, using the usual initialization list today, actually reported an error

struct sales {
    char bookNo;
    unsigned units_sold = 0;
    double revenue = 0.0;
};

int main() {
    sales a = {0};
    // print(cout, a);
}

error message

cpp_primer.cc:62:17: error: could not convert '{0}' from '<brace-enclosed initializer list>' to 'sales'
   62 |     sales a = {0};
      |                 ^
      |                 |
      |                 <brace-enclosed initializer list>

The problem is that the initial value is defined in the structure. At this time, the initialization list cannot be used

Change the structure to

struct sales {
    char bookNo;
    unsigned units_sold;
    double revenue;
};

That’s it

Summary

There are two initialization methods:

    when defining a structure, the initial value is given to initialize the list assignment

    The above two methods are mutually exclusive, and only one can be selected

    reference material https://qa.1r1g.com/sf/ask/2644377641/

Read More: