简体   繁体   中英

Cannot convert Brace-enclosed initializer list

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`

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.

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}};

You can use memset(Table,false,sizeof(Table)) for this.it will work fine.

Here is your complete code

#include <iostream>
#include<cstring>
using namespace std;

   const int dim = 2;
    bool Table[dim][dim];

    int main(){

         memset(Table,true,sizeof(Table));
         cout<<Table[1][0];
// code    
return 0;
    }

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