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}};
Related videos on Youtube
Author by
user3481693
Updated on March 16, 2022Comments
-
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 areg++ -std=c++11
. The error iscannot convert brace-enclosed initializer list to 'bool' in assignment`
-
juanchopanza about 10 yearsYou 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 about 10 yearsmemset with c++ is never a good approach. use fill instead.
-
Ankur about 10 yearsWell i am embedded developer and we use it almost daily.There is no harm in it i think.
-
juanchopanza about 10 yearsBut this doesn't do what OP wants.
-
Mike Seymour about 10 years@Shansingh: In this case,
memset
relies onsizeof(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 ofstd::fill
will be as efficient asmemset
where possible, and will do the right thing whenmemset
won't; so it's better to just use that. -
Ankur about 10 years@juanchopanza :I am just indicating this in term of post initialization.We can use memset in this case.
-
mallwright about 5 yearsRecent versions of GCC warn against the use of memset on class types.