【发布时间】:2013-05-27 01:40:34
【问题描述】:
我希望有人可以帮助我。
我有一个文件,其中列出了许多可以重复的城市。例如:
Lima, Peru
Rome, Italy
Madrid, Spain
Lima, Peru
我用构造函数 City(string cityName) 创建了一个类 City
主要是,我想为每个城市创建一个指针,例如:
City* lima = new City( City("Lima, Peru");
City* rome = new City( City("Rome, Italy");
有没有办法通过循环读取文本中的行来做到这一点,例如:
City* cities = new City[];
int i = 0;
while( Not end of the file )
{
if( read line from the file hasn't been read before )
cities[i] = City(read line from the file);
}
有没有办法,或者我必须为每个人手动完成。有什么建议么?
谢谢
【问题讨论】:
-
不是一个答案,但你使用构造函数的方式有些奇怪。要动态创建
City对象,最好使用City *lima = new City("Lima, Peru");,而不是City *lima = new City(City("Lima, Peru"));。
标签: c++ class visual-c++ pointers variable-assignment