改变对象
Changing Objects
在我正在处理的图表中,我从数据文件中获取了国家,并存储到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,它工作了。谢谢大家的帮助!
相关文章:
- 如何访问和改变存储在矢量C++中的对象
- 通过从构造函数中的'this'复制的指针改变常量对象
- 为什么我可以改变常量对象中的成员变量,这是返回常量对象函数的结果?
- 使用(非对象)API 时改变表数组C++而不重新创建整个平面缓冲区
- 为什么我的函数不改变对象的属性
- 改变对象的原点点使其以不同的轴移动
- 更改指针指向的对象,但对象不会改变
- 对列表中的每个对象调用成员函数,以改变对象所在的列表
- 当函数应该改变对象的状态时,是选择成员函数还是朋友函数
- 一个什么都不做的函数可以改变一个对象吗?
- 改变一个已经压入的列表(在vector对象上)会改变vector对象上的列表吗?
- 如何改变一个c++对象的类(实现可变类型)
- 如果一个引用一旦被初始化为一个对象,它就不能被改变,为什么这样做呢?
- 在c++中,改变指针返回后指向的对象
- 改变数组中的对象,但不改变对象的数量- c++
- 将字符串保存到重载括号中,不改变对象的字段
- 如何用静态方法改变c++中对象的属性
- 我的对象在矢量中的地址改变了
- 在c++中正确的对象强制转换,值字段不会改变
- 当一个对象改变某些值时要意识到