Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Firstly, your variables "a" and "b" are pointers. Your code: layer->tileGids = new int[a][b] is the root cause of the problem. </p> <p>I'm trying to guess your intention here and I think what you are trying to do is make layer.tileGids a 2 dimension array to reference a "grid" of size (mapSize.Width, mapSize.height) so that you can refer to each "cell" in the grid using layer.tileGids[x][y].</p> <p>If you are indeed trying to create a 2 dimension array, there are 2 methods to do it. </p> <p><strong>Method 1:</strong></p> <pre><code>class Layer { int ** tileGids; // NOTE the "**" to indicate tileGids is a pointer to pointer i.e. 2D array. } </code></pre> <p>To initialize it:</p> <pre><code>int width = map-&gt;mapSize.width; int height = map-&gt;mapSize.height; layer.tileGids = new int*[width]; // NOTE the "int*" to indicate tileGids is a new array of pointers to int. for (int i = 0; i &lt; width; i++) // Initialize each element in layer.tileGids[] to be a pointer to int. { layer.tileGids[i] = new int[height]; } </code></pre> <p>Now you can access the items in layer.tileGids using:</p> <pre><code>int value = layer.tileGids[x][y] // where 0 &lt;= x &lt; width and 0 &lt;= y &lt; height </code></pre> <p>To deallocate this data structure, similar to how you allocate it, you need to deallocate each dynamically allocated array in each "row":</p> <pre><code>for (int i = 0; i &lt; width; i++) { delete [] layer.tileGids[i]; // Deallocate each row. } delete [] layer.tileGids; // Deallocate "array" to the pointers itself. </code></pre> <p><strong>Method 2:</strong></p> <p>Now another easier, less messy method (avoid pointers) is to use the C++ vector class. You need to make the following changes:</p> <pre><code>#include &lt;vector&gt; class Layer { vector&lt;vector&lt;int&gt; &gt; tileGids; // Note the space at "&lt;int&gt; &gt;". } </code></pre> <p>To initialize:</p> <pre><code>int width = map-&gt;mapSize.width; int height = map-&gt;mapSize.height; layer.tileGids = vector&lt;vector&lt;int&gt; &gt;(width, vector&lt;int&gt;(height, 0)); // Initialize all entries to 0. </code></pre> <p>To access the elements:</p> <pre><code>int value = layer.tileGids[x][y]; // Where 0 &lt;= x &lt; width and 0 &lt;= y &lt; height </code></pre> <p>Note that for the second method using vectors, you do not have to do any memory cleanup as is required in the first method because the vector will automatically take care of it. However, because a vector can grow dynamically i.e. you can add items to it, you lose the safety of having a fixed size array i.e. someone could accidentally increase the size of your grid if you use the vector method but if he tries to do that when you intialized it using the first method above an error will occur and you will immediately know that something is wrong.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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