Cannot convert Brace-enclosed initializer list

46,629

Solution 1

Arrays can only be initialized like that on definition, you can't do it afterwards.

Either move the initialization to the definition, or initialize each entry manually.

Solution 2

First, you are trying to assign a concrete element of array instead assigning the full array. Second, you can use initializer list only for initialization, and not for assignment.

Here is correct code:

bool Table = {{false,false},{true,false}};
Share:
46,629

Related videos on Youtube

user3481693
Author by

user3481693

Updated on March 16, 2022

Comments

  • user3481693
    user3481693 over 2 years

    I declare a table of booleans and initialize it in main()

    const int dim = 2;
    bool Table[dim][dim];
    
    int main(){
    
         Table[dim][dim] = {{false,false},{true,false}};
         // code    
         return 0;
    }
    

    I use mingw compiler and the builder options are g++ -std=c++11. The error is

    cannot convert brace-enclosed initializer list to 'bool' in assignment`

    • juanchopanza
      juanchopanza about 10 years
      You can't assign to plain arrays. You need a type that supports that (std::array<std::array<bool,2>, 2>), or set the elements by hand.
  • user1810087
    user1810087 about 10 years
    memset with c++ is never a good approach. use fill instead.
  • Ankur
    Ankur about 10 years
    Well i am embedded developer and we use it almost daily.There is no harm in it i think.
  • juanchopanza
    juanchopanza about 10 years
    But this doesn't do what OP wants.
  • Mike Seymour
    Mike Seymour about 10 years
    @Shansingh: In this case, memset relies on sizeof(bool)==1, which isn't guaranteed. More generally, it can only be safely used for trivial types, so you're setting up deathtraps to trigger undefined behaviour if the type changes in future. Any decent implementation of std::fill will be as efficient as memset where possible, and will do the right thing when memset won't; so it's better to just use that.
  • Ankur
    Ankur about 10 years
    @juanchopanza :I am just indicating this in term of post initialization.We can use memset in this case.
  • mallwright
    mallwright about 5 years
    Recent versions of GCC warn against the use of memset on class types.