Note that there are some explanatory texts on larger screens.

plurals
  1. POAdding elements to a vector inside a c++ class not being stored
    text
    copied!<p><b>Edit: My debugger was lying to me. This is all irrelevant</b></p> <p>Howdy all,</p> <p>I had a peek at <a href="https://stackoverflow.com/questions/637438/adding-element-to-vector">Adding element to vector</a>, but it's not helpful for my case.</p> <p>I'm trying to add an element (custom class LatLng) to another object (Cluster) from a third object (ClusterManager). </p> <p>When I pass my LatLng to Cluster (last line of ClusterManager.cpp), and jump into Cluster::addLocation, at the end of the function execution gdb says my new LatLng has been added to Cluster, but the moment I jump back into the scope of the highest class, ClusterManager, the new LatLng added to the vector 'locStore' is not present in either runtime or debug. </p> <p>Any ideas?</p> <p>DJS.</p> <p>DE: Xcode 3.2 (Targeted to Debug 10.5) OS: OSX 10.6 Compiler: GCC 4.2 Arch: x86_64</p> <p><i>ClusterManager.cpp</i> (where it's all being called from):</p> <pre><code>void ClusterManager::assignPointsToNearestCluster() { //Iterate through the points. for (int i = 0; i &lt; locationStore.size(); i++) { double closestClusterDistance = 100.1; // Make sure to chuck the shits if we don't find a cluster. int closestCluster = -1; int numClusters = clusterStore.size(); // Iterate through the clusters. for (int j = 0; j &lt; numClusters; j++) { double thisDistance = locationStore[i].getDistanceToPoint( *(clusterStore[j].getCentroid()) ); // If there's a closer cluster, make note of it. if (thisDistance &lt; closestClusterDistance) { closestClusterDistance = thisDistance; closestCluster = j; } } // Remember the penultiment closest cluster. this-&gt;clusterStore[closestCluster].addLocation( this-&gt;locationStore[i] ); } } </code></pre> <p><i>ClusterManager.h</i></p> <pre><code>#include "Cluster.h" #include "LatLng.h" #include &lt;vector&gt; class ClusterManager{ private: std::vector&lt;Cluster&gt; clusterStore; std::vector&lt;LatLng&gt; locationStore; public: ClusterManager(); void assignPointsToNearestCluster(); void addLocation(int,double,double); }; </code></pre> <p><i>Cluster.h:</i></p> <pre><code>#include &lt;vector&gt; #include &lt;string&gt; #include "LatLng.h" class Cluster { private: std::vector&lt;LatLng&gt; locStore; LatLng newCentroid; bool lockCentroid; int clusterSize; int clusterID; public: Cluster(int,LatLng&amp;); void addLocation(LatLng&amp;); LatLng* getCentroid(); }; </code></pre> <p><i>Cluster.cpp</i></p> <pre><code>Cluster::Cluster(int newId, LatLng &amp;startPoint) { this-&gt;clusterID = newId; this-&gt;newCentroid = startPoint; }; void Cluster::addLocation(LatLng &amp;newLocation) { (this-&gt;locStore).push_back( newLocation ); }; LatLng* Cluster::getCentroid() { return &amp;newCentroid; }; </code></pre>
 

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