samedi 24 janvier 2015

error: too many initializers for const

Why do I get the following error



$ g++ -std=c++11 aaa.cpp



aaa.cpp:15:2: error: too many initializers for ‘const aaaa::Fruit_List [0]’


};


^



when compiling the following code:



class aaaa
{ // without wrapping in class the code compiles fine
const int a=7; // compiles fine

struct Fruit_List{
int index;
int length;
} const fruit_list[]={ // error: if I put 5 in braket the code compiles fine
{0,3},
{1,2},
{2,5},
{3,1},
{4,7}
};
};

int main()
{
return 0;
}


If I write the code without wrapping in the class, it would compile fine. Giving the length of the array will suppress compiler error. But insist of putting the code inside the class and avoid giving the array size as I may add any member later and I want to leave the array length determination to the compiler.


Strongly, please avoid linking to any inappropriate question.


Update


Thanks to juanchopanza's comment. Now I know that even this simpler code does not compile:



class aaaa
{
const int a[]={7,4,5};
};

int main()
{
return 0;
}

Aucun commentaire:

Enregistrer un commentaire