After reading How to initialize an array in C , in particular:
Do not lose sight of the obvious solution:
int myArray [10] = {5, 5, 5, 5, 5, 5, 5, 5, 5, 5};
I tried something like this:
#include <iostream> class Something { private: int myArray[10]; public: Something() { myArray[10] = { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 }; } int ShowThingy(int what) { return myArray[what]; } ~Something() {} }; int main () { Something Thing; std::cerr << Thing.ShowThingy(3); }
And I get:
..\src\Something.cpp: In constructor 'Something::Something()': ..\src\Something.cpp:10:48: error: cannot convert '<brace-enclosed initializer list>' to 'int' in assignment
The obvious in this case is not so obvious. I really would like the initialization of my array to be more dynamic.
I'm tired:
private: int * myArray; public: Something() { myArray = new int [10]; myArray = { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 }; }
This looked funny to me, and therefore to the compiler:
..\src\Something.cpp: In constructor 'Something::Something()': ..\src\Something.cpp:11:44: error: cannot convert '<brace-enclosed initializer list>' to 'int*' in assignment
This also did not work:
private: int myArray[10] = { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 };
from:
..\src\Something.cpp:6:20: error: a brace-enclosed initializer is not allowed here before '{' token ..\src\Something.cpp:6:51: sorry, unimplemented: non-static data member initializers ..\src\Something.cpp:6:51: error: 'constexpr' needed for in-class initialization of static data member 'myArray' of non-integral type
I am doing really well and learning what is not working, but not so good studying what works.
So, how did I use the initialization lists {value, value, value} for the array inside the class?
I tried to figure out how to do this for some time, and I was very stuck, I have several such lists that I need to do for my application.