C++ 改变对象

C++ 改变对象,c++,C++,在我正在处理的一个图表中,我从数据文件中获取了国家,并将其存储到CCountry对象中,然后这些国家存储在CContinent对象中,这些对象存储在称为world的CContinent列表中。当我在循环中查找他们添加的国家的信息时,它为我提供了正确的信息。但是,如果我以后再查国家信息,就会发现所有国家都是同一个国家(最后添加的国家)。这是我创建国家/地区列表的代码,以及相关的目标代码 while(line != "------") { getline(filestr, line);

在我正在处理的一个图表中,我从数据文件中获取了国家,并将其存储到CCountry对象中,然后这些国家存储在CContinent对象中,这些对象存储在称为world的CContinent列表中。当我在循环中查找他们添加的国家的信息时,它为我提供了正确的信息。但是,如果我以后再查国家信息,就会发现所有国家都是同一个国家(最后添加的国家)。这是我创建国家/地区列表的代码,以及相关的目标代码

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

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



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



CCountry::CCountry(string in_name)
{
name = in_name;
}
while(行!=“----”)
{
getline(filestr,line);
CContinent*tempContinent=新CContinent(行);
getline(filestr,line);
而(行!=”--“&&line!=”--“)
{
CCountry*tempCountry=新CCountry(行);
临时大陆->添加国家(*临时国家);
getline(filestr,line);
//cout getName()getCountries();
对于(列表::迭代器it2=var1.begin();it2!=var1.end();it2++)
{

cout您的问题在于如何传递指针和引用。您有一个
列表
CCountry*

list<CCountry*> countries;

您的问题在于如何传递指针和引用。您有一个
CCountry*
列表:

list<CCountry*> countries;

新的错误更新:这是一个类似于大陆addCountry的错误。我将CCCountry中的addNeighbor函数更改为addCountry,并且它工作了。感谢大家的帮助!

新的错误更新:这是一个类似于大陆addCountry的错误。我将CCCountry中的addNeighbor函数更改为addCountry,并且它工作了。谢谢大家的帮助!

我想最重要的部分是你在哪里做
随时[随时]查找国家信息稍后
让我们来看看你的
CContinet
声明,它是
国家
成员。这可能是因为你的
&
在推后过程中发生的。我想最相关的部分是你在哪里做
随时查找国家信息稍后
让我们看一下
CContinet
的声明,它是
国家
的成员。这可能是因为你的
&
在推送中发生的。这修复了它,但代码仍然有一个bug。谢谢你的帮助。NP。我想如果你遇到的问题与问题中讨论的不同,你可以总是开始一个新的问题:]它现在看起来确实在工作。我正在做最后一个测试,看看图表是否正确构建,但我感到非常乐观。我想你以前在这方面帮助过我,所以请享受你的向上投票。:-)这修复了它,但代码仍然存在错误。谢谢你的帮助。NP。我想如果你有不同的pr与问题中讨论的问题相比,您始终可以开始一个新问题:]它实际上现在似乎起作用了。我正在做最后一次测试,以查看图表是否正确构建,但我感到非常乐观。我认为您在此之前已经帮助过我,所以请享受您的投票。:-)
for (list<CContinent*>::iterator it = world.begin(); it != world.end(); it++)
{
    list<CCountry*> var1 = (*it)->getCountries();
    for (list<CCountry*>::iterator it2 = var1.begin(); it2 != var1.end(); it2++)
    {
        cout << "Country: " << (*it2)->getName() << endl;
        list<CCountry*> var2 = (*it2)->getNeighbors();
        cout << "Neighbors: ";
        for (list<CCountry*>::iterator it3 = var2.begin(); it3 != var2.end(); it3++)
        {
            cout << (*it3)->getName() << " ";
        }
        cout << endl;
    }
}   
for (list<CContinent*>::iterator it = world.begin(); it != world.end(); it++)
{
    list<CCountry*> var1 = (*it)->getCountries();
    for (list<CCountry*>::iterator it2 = var1.begin(); it2 != var1.end(); it2++)
    {
        cout << "Country: " << (*it2)->getName() << endl;
        list<CCountry*> var2 = (*it2)->getNeighbors();
        cout << "Neighbors: ";
        for (list<CCountry*>::iterator it3 = var2.begin(); it3 != var2.end(); it3++)
        {
            cout << (*it3)->getName() << " ";
        }
        cout << endl;
    }
}   
list<CCountry*> countries;
void CContinent::addCountry(CCountry* country)
{
    countries.push_back(country);
}