【问题标题】:How to make ostream operator for nested list in c++?如何在 C++ 中为嵌套列表制作 ostream 运算符?
【发布时间】:2020-04-13 15:35:44
【问题描述】:
  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;
    list<list<CTurist>>l;
public:
    CHotel()
    {
        hotel_name = "";
        num_beds = 0;
        aver_price = 0;
    }
    CHotel(string hn, int nb, double ap, list<list<CTurist>>&lis)
    {
        hotel_name = hn;
        num_beds = nb;
        aver_price = ap;
        l = lis;
    }
    CHotel(const CHotel& h)
    {
        hotel_name = h.hotel_name;
        num_beds = h.num_beds;
        aver_price = h.aver_price;
    }
    string get_hotel_name()
    {
        return hotel_name;
    }
    int get_num_beds()
    {
        return num_beds;
    }
    double get_aver_price()
    {
        return aver_price;
    }
    list<list<CTurist>> get_list_name() {
        return l;
    }
    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 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(list<list<CTurist>>::iterator itr=h.l.begin();itr!=h.l.end();itr++)
        {
            for (list<CTurist>::iterator it = itr->begin(); it != itr->end(); it++)
                ostr << *it;
        }
        return ostr;
    }

}


如您所见,我有这 2 个课程 - CTurist 和 CHotel。
第一个的一切都很好,但是第二个出现了问题。
我正在尝试为 CHotel 类制作 ostream 运算符。
其他一切都在工作,但只有这个运营​​商是问题所在。
我确定这是因为嵌套列表,因为它对我来说是新的,也许我错了。

如果有人知道怎么做,请告诉我我的错误在哪里。

<br>And this is the error when im trying to debug it.It sure is from this nested list.

error C2440: 'initializing': cannot convert from 'std::_List_const_iterator<std::_List_val<std::_List_simple_types<_Ty>>>' to 'std::_List_iterator<std::_List_val<std::_List_simple_types<_Ty>>>' 1> with 1> [ 1> _Ty=std::list<CTurist,std::allocator<CTurist>> 1> ]

【问题讨论】:

  • 你覆盖了ostream&amp; operator&lt;&lt;(ostream&amp;, const CTurist&amp;) 吗?
  • @Jarod42 error C2440: 'initializing'(这是 ostream 运算符中第一个 for 循环的行)
  • @JohnnyMopp 不,我没有。 CTurist 类的 ostream 运算符不同,它正在工作,但 CHotel 类的这个不是。
  • 请在问题中包含完整的错误消息。 C2440 是特定于编译器的,不知道您使用什么编译器并没有什么帮助,“初始化”不是完整的消息
  • @Jarod42 是的,它存在于 CTurist 类中。

标签: c++ list class nested ostream


【解决方案1】:

由于hconst CHotel&amp;,你需要const_iterator

for (list<list<CTurist>>::const_iterator itr=h.l.begin();itr!=h.l.end();itr++)
{
    for (list<CTurist>::const_iterator it = itr->begin(); it != itr->end(); it++)
        ostr << *it;
}

或者干脆用for range

for (const auto& turists : h.l)
{
    for (const auto& turist : turists)
        ostr << turist;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-10-07
    • 1970-01-01
    • 2021-08-28
    • 1970-01-01
    • 1970-01-01
    • 2018-03-31
    • 1970-01-01
    • 2011-08-27
    相关资源
    最近更新 更多