Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You're mixing up pointers and references (which is understandable when you're comming from java/net). In C++, there is a clear distinction between:</p> <ol> <li><code>Cell x;</code>, an object (instance) of class <code>Cell</code></li> <li><code>Cell *x;</code>, a pointer to an instance of <code>Cell</code></li> <li><code>Cell &amp;x;</code>, a reference ("another name") to an instance of <code>Cell</code></li> </ol> <p>Java "references" are equivalent to C++ pointers.</p> <p>In your case, there are several issues. First, <code>setLeft()</code> needs to get the address of its argument, like this:</p> <pre><code>void setLeft(const Cell &amp; left) { this-&gt;left = &amp;left; } </code></pre> <p>Second, unless the class <code>Cell</code> is defined in a namespace <code>Cell</code>, the vector would have to be typed just <code>std::vector&lt;Cell&gt; cells;</code>.</p> <p>However, even more dangerous is the fact that your vector contains <em>statically</em> allocated <code>Cell</code> objects (its template argument is a class type, not a pointer). <code>std::vector</code> is allowed to move objects around in memory when it's added to, so once this happens, all existing pointers to objects stored in <code>cells</code> will become dangling. What you should do is store pointers in the vector instead:</p> <pre><code>std::vector&lt;Cell*&gt; cells; cells[pos]-&gt;setLeft(*cells[left]); //assuming setLeft() still takes a reference </code></pre> <p>It might actually be even better to use smart pointers (<code>std::shared_ptr</code> and <code>std::weak_ptr</code>) instead of raw pointers, but that's a separate topic.</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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. 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