【问题标题】:Why i can not create vector objects from text file?为什么我不能从文本文件创建矢量对象?
【发布时间】:2020-08-08 02:50:41
【问题描述】:
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;
                for (int j = 0; j < n; i++) {
                    istr >> tur;
                    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;
    }
}


所以这是我的学校项目。我在读取对象(向量的向量)时遇到问题,并且我的程序没有运行。问题出在 CHotel 类中的朋友 ostream 运算符内部。
我将上传 2 张图片。在第一张您将看到我的文本文件,在第二张您将看到我的错误列表。
我尝试使用 & 修复它,如错误列表中的建议所示,但它没有帮助。
有人能告诉我如何解决吗?
text file

Albena
玉兰 54 110
3
伊万 bg 50
玛丽亚 bg 40
pesho pl 10
3
瓦莱里英国 15
ivelina 英国 35
gosho pl 31
3
詹姆斯 美国 52
卡特琳 45 美国
41 号码头
3
raluca ro 60
金美国 20
威廉 美国 18
3
lenka cz 20
马丁 cz 26
阿德里安 pl 65
3
kalina ro 45
莫妮卡 sp 40
jared 美国 49
3
丹尼尔 bg 17
teq bg 23
丹妮拉 bg 30
3
米伦 bg 43
瓦伦丁 bg 43
安德烈 bg 21
3
阿德里安娜 美国 19
gergana 英国 21
vasilena bg 30
3
vanq bg 23
venci bg 30
graciela bg 21
3
todorka bg 46
stanislav bg 34
马丁娜 bg 65
3
康拉德美国 26
borko ro 45
朱莉娅英国 31

【问题讨论】:

  • friend istream&amp; operator &gt;&gt; (istream&amp; istr, CHotel&amp; h),你有v[i].push_back(tur);,但应该是h.v[i].push_back(tur);…… PS:请不要发布错误图片,而是将错误复制到问题中
  • 请提供文本作为文本,而不是图像。
  • @ChrisMM 好的,我现在将在下一篇文章中,但是当我添加 h.在向量之前这是我得到的消息:Debug Assertion Failed
  • @Samantha 那是因为向量 h.v 的大小为零。因此,当您执行h.v[i] 时,您会收到错误消息。
  • @john 那我应该如何修复它?

标签: c++ file class vector


【解决方案1】:

对于您的原始问题,您会收到有关对非静态成员的引用的错误,因为在您的

friend istream& operator >> (istream& istr, CHotel& h)

函数,你有以下行:

v[i].push_back(tur);

由于此函数实际上不是CHotel 类的一部分,因此v 不能以这种方式访问​​。必须改为:

h.v[i].push_back(tur);

正如 cmets 中所指出的,这会导致断言失败(在 VS 调试版本中)。这是 UB,因为 v 实际上大小为零。在外循环中,您需要将新的vector 添加到v 向量中。因此,最终,您将拥有:

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

PS:你在for循环内部也有i++,应该是j++

【讨论】:

  • 谢谢你的解释,我终于明白了!我试着像你说的那样修复它,但又一次:Debug Assertion Failed on the same line where push_back tur
  • 您可以将文件内容作为文本发布吗?
  • Project2.exe 中 0x5114ED76 (ucrtbased.dll) 处未处理的异常:将无效参数传递给认为无效参数致命的函数。这是在我说的那一行的程序中..这很奇怪
  • 我发布了我的整个文本文件
  • 非常感谢!最后一个问题:如果我的文本文件内容不仅在 1 行中,因为我的 txt 在很多行中,这有关系吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-30
  • 1970-01-01
  • 2019-04-07
  • 2011-08-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多