Note that there are some explanatory texts on larger screens.

plurals
  1. POHow am I accidentally overwriting when referencing these pointers?
    primarykey
    data
    text
    <p>Last question for tonight, I promise. These pointers are giving me a serious headache.</p> <p>I have a std::list<code>&lt;Point</code>> called Polygon and a std::list of Polygons defined like:</p> <pre><code>typedef std::list&lt;Point&gt; Polygon; typedef std::list&lt;Polygon&gt; PolygonList; // List of all our polygons PolygonList polygonList; </code></pre> <p>I created the method below to attempt to delete the nearest Point from an (x,y), checking all of my Polygons within my polygonList.</p> <pre><code>void deleteNearestPoint(int x, int y) { y = screenHeight - y; Polygon &amp;closestPolygon = polygonList.front(); Polygon::iterator closestPoint = closestPolygon.begin(); float closestDistance = sqrt(pow(x - closestPoint-&gt;x, 2) + pow(y - closestPoint-&gt;y, 2)); // Search PolygonList PolygonList::iterator listIter; Polygon::iterator iter; for(listIter = polygonList.begin(); listIter != polygonList.end(); listIter++) { Polygon &amp;tempPolygon = *listIter; for(iter = tempPolygon.begin(); iter != tempPolygon.end(); iter++) { const float distance = sqrt(pow(x - iter-&gt;x, 2) + pow(y - iter-&gt;y, 2)); if (distance &lt; closestDistance) { closestPolygon = *listIter; closestPoint = iter; closestDistance = distance; } } } closestPolygon.erase(closestPoint); redraw(); } </code></pre> <p>However, somewhere I have a pointer or reference variable screwing me up. This code compiles but acts in a very strange way.</p> <p>I've written a debug statement and lets say I have 3 polygons in my polygon list like so:</p> <blockquote> <p>Polygon #: 0<br> Point: (448, 43)<br> Point: (469, 177)<br> Point: (374, 123)<br> Polygon #: 1<br> Point: (295, 360)<br> Point: (422, 350)<br> Point: (315, 266)<br> Point: (295, 360)<br> Polygon #: 2<br> Point: (143, 202)<br> Point: (301, 203)<br> Point: (222, 100)<br> Point: (143, 202) </p> </blockquote> <p>Now, lets say I try and use the delete function giving it an x/y close to point 422, 350 The desired result would be it simply deleting that point (422, 350) from Polygon #1 but instead I get this:</p> <blockquote> <p>Polygon #: 0<br> Point: (295, 360)<br> Point: (422, 350)<br> Point: (315, 266)<br> Point: (295, 360)<br> Polygon #: 1<br> Point: (295, 360)<br> Point: (315, 266)<br> Point: (295, 360)<br> Polygon #: 2<br> Point: (143, 202)<br> Point: (301, 203)<br> Point: (222, 100)<br> Point: (143, 202) </p> </blockquote> <p>It did delete (422, 350) but it also has the strange side effect of overwriting Polygon#0 to what Polygon#1 was before the delete of its point.</p> <p>I know I'm using a pointer or reference incorrectly in my method. Can someone point out what I could possibly be doing that is causing this? I think it is because my &amp;closestPolygon is declared as a reference, but I get compile errors if I try to set it as anything else.</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.
 

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