改变对象

Changing Objects

本文关键字:对象 改变      更新时间:2023-10-16

在我正在处理的图表中,我从数据文件中获取了国家,并存储到CCountry对象中,然后这些存储在CContinent对象中,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;
}

请询问您想看到的任何其他代码。

每个请求,这里是我稍后尝试访问列表的地方。

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

CContinent声明:

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

新错误:

所以它现在运行整个程序,这很好,但是我在main中添加了一个测试输出,看看它是否得到了所有的邻居。现在的问题是,它得到了正确的邻居数量,但当被问到他们的名字时,它只显示倒数第二个国家。什么好主意吗?

作为参考,下面是代码。

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;
    }
}   

你的问题是你如何传递指针和引用。你有CCountry*List:

list<CCountry*> countries;

所以你的addCountry函数应该看起来更像:

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

新的错误更新:这是一个类似的错误在大陆的addCountry。我改变了CCountry中的addNeighbor函数,看起来像addCountry,它工作了。谢谢大家的帮助!