Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You wrote this</p> <pre><code>for (int k = 0; k &lt; 13; k++) { for (int j = 0; j&lt; 1; j++) { for (int i = 0; i&lt; 4; i++) { cabins[13][4][1] = (("b" "i" "i" "b") </code></pre> <p>Which would iterate across all the characters in the array, and then try to assign the entire array that slot, if it worked, or was valid C++.</p> <p>I'm not sure what "(("b" "i" "i" "b")" is supposed to be, but you seem to have missed some C++ fundamentals. What you actually want is something more like</p> <pre><code>char cabins[13][4]; // only needs to be 2d. void Ship::setArray() { cabins = { { 'b', 'i', 'i', 'b' }, { 'b', 'i', 'i', 'b' }, ... }; } </code></pre> <p>[edit: I hit return early, working on the laptop, sorry]</p> <p>This too would not work. If the array definition here is to be persistent, you'll need to store it somewhere.</p> <p>Here's a complete single-compilation-unit example of how you might solve it:</p> <pre><code>#include &lt;iostream&gt; #include &lt;cstring&gt; // for memcpy class Ship { public: Ship() {} char m_cabins[4][4]; void setArray(); }; void Ship::setArray() { static const char defaultCabinLayout[4][4] = { { 'b', 'i', 'i', 'b' }, { 'b', 'i', 'i', 'b' }, { 'w', 'i', 'i', 'w' }, { 'w', 'i', 'i', 'w' }, }; static_assert(sizeof(m_cabins) == sizeof(defaultCabinLayout), "defaultCabinLayout does not match m_cabins"); memcpy(m_cabins, defaultCabinLayout, sizeof(m_cabins)); } int main() { Ship s; s.setArray(); std::cout &lt;&lt; "cabins[0][0] = " &lt;&lt; s.m_cabins[0][0] &lt;&lt; std::endl; } </code></pre>
 

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