Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to initialize an array in C++ objects
    primarykey
    data
    text
    <p>After reading <a href="https://stackoverflow.com/questions/201101/how-to-initialize-an-array-in-c">How to initialize an array in C</a>, in particular:</p> <blockquote> <p>Don't overlook the obvious solution, though:</p> <p>int myArray[10] = { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 };</p> </blockquote> <p>I tried something like this:</p> <pre><code>#include &lt;iostream&gt; 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 &lt;&lt; Thing.ShowThingy(3); } </code></pre> <p>And I get:</p> <pre><code>..\src\Something.cpp: In constructor 'Something::Something()': ..\src\Something.cpp:10:48: error: cannot convert '&lt;brace-enclosed initializer list&gt;' to 'int' in assignment </code></pre> <p>The obvious in this case is not so obvious. I really would like the initiation of my array to be more dynamic as well.</p> <p>I tired:</p> <pre><code>private: int * myArray; public: Something() { myArray = new int [10]; myArray = { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 }; } </code></pre> <p>This looked funky to me to, and so to the compiler:</p> <pre><code>..\src\Something.cpp: In constructor 'Something::Something()': ..\src\Something.cpp:11:44: error: cannot convert '&lt;brace-enclosed initializer list&gt;' to 'int*' in assignment </code></pre> <p>This also did not work:</p> <pre><code>private: int myArray[10] = { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 }; </code></pre> <p>with:</p> <pre><code> ..\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 </code></pre> <p>I have been doing really good and learning what does not work, but not so good learning what does work.</p> <p>So, how do I used initialization lists {value, value, value} for an array inside a class?</p> <p>I have been trying to figure out how to do this for some time now and am very stuck, I have a number of these kinds of lists I need to make for my app.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload