Changing Objects
<p>In a graph I'm working on, I have countries taken in from a data file, and stored into CCountry objects, then those are stored in CContinent objects, which are stored in a list of CContinents called world. When I look up information on the countries in the loop during which their added, it gives me the correct information. However, if I look up country info any later, it says all countries are the same country (the last country added). Here's the code where I create the country list, and relevant object code.</p>

<pre><code>while(line != "------")
{
    getline(filestr, line);
    CContinent *tempContinent = new CContinent(line);

    getline(filestr, line);
    while(line != "---" &amp;&amp; line != "------")
    {
        CCountry *tempCountry = new CCountry(line);
        tempContinent-&gt;addCountry(*tempCountry);
        getline(filestr, line);
        //cout &lt;&lt; tempCountry-&gt;getName() &lt;&lt; flush;
    }
    world.push_back(tempContinent);
}



void CContinent::addCountry(CCountry country)
{
    (countries).push_back(&amp;country);
}



CCountry::CCountry(string in_name)
{
name = in_name;
}
</code></pre>

<p>Please ask for any other code you'd like to see. </p>

<p>per request, here is where I try and access the lists later.</p>

<pre><code>homeCountry = NULL;     

    size_t found;
    found = line.find_first_of(",");
    string homeCountryName = line.substr(0, found);
    for (list&lt;CContinent*&gt;::iterator it=world.begin(); it != world.end(); it++)
    {
        list&lt;CCountry*&gt; tempCont = (*it)-&gt;getCountries();
        //cout &lt;&lt; it-&gt;getName() &lt;&lt; " " &lt;&lt; flush;
        for (list&lt;CCountry*&gt;::iterator it2=tempCont.begin(); it2 != tempCont.end(); it2++)
        {
            //cout &lt;&lt; (*it2)-&gt;getName() &lt;&lt; flush;
            //cout &lt;&lt; homeCountryName &lt;&lt; flush;
            if ((*it2)-&gt;getName() == homeCountryName)
            {
                *homeCountry = **it2;
            }
        }
    }
</code></pre>

<p>CContinent declarations:</p>

<pre><code>class CContinent
{
string name;
list&lt;CCountry*&gt; countries;
public:
CContinent(string in_name);
~CContinent();
void addCountry(CCountry country);
list&lt;CCountry*&gt; getCountries();
string getName();
};
</code></pre>

<p>NEW BUG:</p>

<p>So it runs through the entire program now, which is nice, but I added a test output in main to see if it was getting all the neighbors correct. The problem now is that it's getting the correct number of neighbors, but when asked what their names are, it just says the second to last country. Any ideas?</p>

<p>For reference, here's the code.</p>

<pre><code>for (list&lt;CContinent*&gt;::iterator it = world.begin(); it != world.end(); it++)
{
    list&lt;CCountry*&gt; var1 = (*it)-&gt;getCountries();
    for (list&lt;CCountry*&gt;::iterator it2 = var1.begin(); it2 != var1.end(); it2++)
    {
        cout &lt;&lt; "Country: " &lt;&lt; (*it2)-&gt;getName() &lt;&lt; endl;
        list&lt;CCountry*&gt; var2 = (*it2)-&gt;getNeighbors();
        cout &lt;&lt; "Neighbors: ";
        for (list&lt;CCountry*&gt;::iterator it3 = var2.begin(); it3 != var2.end(); it3++)
        {
            cout &lt;&lt; (*it3)-&gt;getName() &lt;&lt; " ";
        }
        cout &lt;&lt; endl;
    }
}   
</code></pre>

<p>NEW BUG:</p>

<p>So now it runs through the entire program, which is a plus. However, when I ask it to give me the name of the neighboring countries in the main of the program, it gives me a list of the (generally) correct number of neighboring countries, with all the name of the second to last country. Not sure why. Here's the code.</p>

<pre><code>for (list&lt;CContinent*&gt;::iterator it = world.begin(); it != world.end(); it++)
{
    list&lt;CCountry*&gt; var1 = (*it)-&gt;getCountries();
    for (list&lt;CCountry*&gt;::iterator it2 = var1.begin(); it2 != var1.end(); it2++)
    {
        cout &lt;&lt; "Country: " &lt;&lt; (*it2)-&gt;getName() &lt;&lt; endl;
        list&lt;CCountry*&gt; var2 = (*it2)-&gt;getNeighbors();
        cout &lt;&lt; "Neighbors: ";
        for (list&lt;CCountry*&gt;::iterator it3 = var2.begin(); it3 != var2.end(); it3++)
        {
            cout &lt;&lt; (*it3)-&gt;getName() &lt;&lt; " ";
        }
        cout &lt;&lt; endl;
    }
}   
</code></pre>
