Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Try to narrow down the error by removing all unnecessary code (object copy, vector access...). Then if the problem really comes from <code>erase</code>, add guards to be sure the indexes are right. Try again with this code:</p> <pre><code>#include &lt;cassert&gt; void DominoCollection::drawDomino( void ) { assert(dominoes.size() != 0 &amp;&amp; "Your vector is empty"); int index = random.nextNumber( dominoes.size() ); assert(index &lt; dominoes.size() &amp;&amp; "Buffer overflow..."); dominoes.erase( dominoes.begin() + index ); } </code></pre> <p>If it still segfaults, the problem comes from the destructor of the class <code>Domino</code>. Look if there is anything fishy there. You can do quick tests by commenting parts of the destructor (or all of it), to know where the problem comes from. Calling <code>erase</code> will call the destructor of <code>Domino</code>.</p> <p>If the destructor is not implemented, it might be a good idea to implement one with an output in it (on <code>cerr</code>, not <code>cout</code>) to know if it gets there or crashes earlier on. Is <code>Domino</code> a derived class? Does it have any other objects in it or only a composition of primitive types?</p> <p><strong>Edit</strong> </p> <p>I went through your code quickly: the problem comes from the assignment operator:</p> <pre><code>Domino &amp; Domino::operator = ( const Domino &amp; d ) { *this = d; } </code></pre> <p>This is <strong>NOT</strong> how it should be written... I let you debug that as an exercise.</p> <p>As for why that is the source of the bug: you said <code>erase</code> crashed but not <code>pop_back</code>. The main difference between the two (implementation difference, not the obvious semantic difference) is that erasing causes all the elements to shift after the ones deleted (using the <code>=</code> operator) because <code>std::vector</code> requires the elements to be stored consecutively. Whereas pop just changes the tail pointer and does not alter the rest of the container.</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.
    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