为什么我无法从文件读取所有行并创建对象?

class CTurist
{
private:
    string name;
    string country;
    int age;
public:
    CTurist()
    {
        name = "";
        country = "";
        age = 0;
    }
    CTurist(string n, string c, int a)
    {
        name = n;
        country = c;
        age = a;
    }
    CTurist(const CTurist &t)
    {
        name = t.name;
        country = t.country;
        age = t.age;
    }
    string get_name()
    {
        return name;
    }
    string get_country()
    {
        return country;
    }
    int get_age()
    {
        return age;
    }
    void set_name(string n)
    {
        name = n;
    }
    void set_country(string c)
    {
        country = c;
    }
    void set_age(int a)
    {
        age = a;
    }
    bool operator<(CTurist& t)
    {
        return this->age < t.age;
    }
    friend istream& operator >> (istream& istr, CTurist& t)
    {
        istr >> t.name >> t.country >> t.age;
        return istr;
    }
    friend ostream& operator<<(ostream& ostr, const CTurist& t)
    {
        ostr << "\nName: " << t.name << ", country: " << t.country << ", age: " << t.age;
        return ostr;
    }
};

class CHotel
{
private:
    string hotel_name;
    int num_beds;
    double aver_price;
    vector<vector<CTurist>>v;
public:
    CHotel()
    {
        hotel_name = "";
        num_beds = 0;
        aver_price = 0;
    }
    CHotel(string hn, int nb, double ap, vector<vector<CTurist>>&vec)
    {
        hotel_name = hn;
        num_beds = nb;
        aver_price = ap;
        v = vec;
    }
    CHotel(const CHotel& h)
    {
        hotel_name = h.hotel_name;
        num_beds = h.num_beds;
        aver_price = h.aver_price;
        v = h.v;
    }
    string get_hotel_name()
    {
        return hotel_name;
    }
    int get_num_beds()
    {
        return num_beds;
    }
    double get_aver_price()
    {
        return aver_price;
    }
    vector<vector<CTurist>> get_vector() {
        return v;
    }
    void set_hotel_name(string hn)
    {
        hotel_name = hn;
    }
    void set_num_beds(int nb)
    {
        num_beds = nb;
    }
    void set_aver_price(double ap)
    {
        aver_price = ap;
    }
    bool operator<(CHotel& h)
    {
        return this->aver_price < h.aver_price;
    }
    friend istream& operator >> (istream& istr, CHotel& h)
    {
    int n;
    CTurist tur;
    istr >> h.hotel_name >> h.num_beds >> h.aver_price;
    for (int i = 0; i <= 11; i++) {
        istr >> n;
        h.v.push_back({});
        for (int j = 0; j < n; j++) {
            istr >> tur;
            h.v[i].push_back(tur);
        }
    }
    return istr;
    }
    friend ostream& operator<<(ostream& ostr, const CHotel& h)
    {
        ostr << "Hotel name: " << h.hotel_name << ", number of beds: " << h.num_beds << ", average price: " << h.aver_price;
        for(const auto& it: h.v)
        {
            for (const auto& itr : it)
                ostr << itr;
        }
        return ostr;

    }
    void Output(ostream &ostr)
    {
        ostr << "\nHotel name: " << hotel_name << ", number of beds: " << num_beds << ", average price: " << aver_price;
        for (const auto& it : v)
        {
            for (const auto& itr : it)
                ostr << itr;
        }
    }
};

class CTurLine
{
    string m_line;
    vector<CHotel>m_hoteli;
public:
    CTurLine(string l, vector<CHotel>& vec)
    {
        m_line = l;
        m_hoteli = vec;
    }
    CTurLine(const string& filename)
    {
        ifstream ifile(filename);
        if (ifile.is_open())
        {
            ifile >> m_line;
            copy(istream_iterator<CHotel>(ifile), istream_iterator<CHotel>(), back_inserter(m_hoteli));
        }
    }
    friend ostream& operator<<(ostream& ostr, const CTurLine& t)
    {
        ostr << "Turline: " << t.m_line;
        for (const auto& it : t.m_hoteli)
        {
            ostr << it;
        }
        return ostr;
    }
    void Output(ostream &ostr)
    {
        ostr << m_line << endl;
        copy(m_hoteli.begin(), m_hoteli.end(), ostream_iterator<CHotel>(ostr, " "));
    }
};
int main()
{
    try {
        CTurLine tur("data.txt");
        tur.Output(cout);
    }
    catch (exception &e)                //catch(char *c) -AKO E SAMO THROW
    {                                  //{
        cout << e.what() << endl;     //cout<<c<<endl;
    }
}

这是txt: 阿尔贝纳 玉兰54110 3 伊万bg 50 玛丽亚bg 40 佩肖10 3 瓦莱里英国15 伊维利纳英国35 gosho pl 31 3 詹姆斯·美国52 卡特琳45美国 码头fr 41 3 拉卢卡罗60 金美国20 美国威廉18 3 伦卡20 马丁CZ 26 阿德里安pl 65 3 卡利纳罗45 莫妮卡sp 40 贾里德美国49 3 丹尼尔·伯格17 特克bg 23 丹妮拉bg 30 3 米兰43 瓦伦丁bg 43 安德烈bg 21 3 阿德里亚娜美国19 杰加纳(英国)21 瓦西莱纳bg 30 3 Vanq BG 23 芬奇bg 30 graciela bg 21 3 托多尔卡bg 46 斯坦尼斯拉夫bg 34 马丁娜BG 65 3 美国康拉德26 博尔科ro 45 朱莉娅英国31 Albena是m_line,木兰是hotel_name,54是num_beds,110是aver_price 在下一行是数字3,这意味着向量中将仅添加3位游客 有12次(每月12个向量)。 一切都很好,但是当我运行程序时,它只显示输出->“ Albena”(m_line)。 有人可以告诉我该如何解决? 我只能从文本文件中打印第一行的问题在哪里?

评论