Note that there are some explanatory texts on larger screens.

plurals
  1. POAccessing a property of a pointer in a struct
    primarykey
    data
    text
    <p>First of I have 2 Classes in 2 files (both .h and .cpp files), <em>Create.h</em> and <em>AI.h</em>.</p> <p>Create.h has this struct in it:</p> <pre><code>public: struct Cell { int row; int column; vector&lt;Cell*&gt; neighbors; State state; }; </code></pre> <p>Here is the enum class State (stored in the Create.h file):</p> <pre><code>enum class State : char { visited, empty }; </code></pre> <p>In AI.h I have a stack and a Create object like this:</p> <pre><code>private: deque&lt;Create::Cell*&gt; path; Create obj; </code></pre> <p>In AI.cpp there is an instance called <em>obj</em> which contains a 2D array of Cell where every Cell has row, column, state and neighbors set appropriately.</p> <p>Constructor:</p> <pre><code>AI::AI(Create createObj) : obj(createObj) { start = &amp;obj.array[1][1]; } </code></pre> <p>I also have a function like this which starts with <code>start</code> as an argument:</p> <pre><code>void AI::Function(Create::Cell* cell) { path.push_back(cell); cell-&gt;state = State::visited; while (expression) { cell = path.back(); cell-&gt;state = State::visited; for (int i = 0; i &lt; cell-&gt;neighbors.size(); i++) { if (cell-&gt;neighbors[i]-&gt;state == State::empty) { path.push_back(cell-&gt;neighbors[i]); } } } } </code></pre> <p>So basically the cell in the path stack starts by pointing to the obj.array[1][1] and sets it correctly (I have confirmed that it actually points where I want it to) but when I retrieve a cell from the stack it doesn't point to the obj.array.</p> <p>What I am trying to do is to set the state of the neighbors of each cell but I manage to set the state on the <em>path</em> stack, on the cell itself but NOT on the array inside the obj instance.</p> <p>What I definitely can do and it works is set the obj.array[i][j] under the cell like this:</p> <pre><code>cell = path.back(); cell-&gt;state = State::visited; obj.array[cell-&gt;row][cell-&gt;column].state = State::visited; </code></pre> <p>What I wanted to know is if there is a way (through pointers I presume) to make the obj.array Cell element know that it's being set by the <em>cell->state = State::visited;</em></p>
    singulars
    1. This table or related slice is empty.
    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. This table or related slice is empty.
    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