【问题标题】:Reading a file into a vector of class objects将文件读入类对象的向量
【发布时间】:2018-05-01 11:29:47
【问题描述】:

根据我在众多论坛和书籍上阅读的内容,我已经尝试了几次。有关此作业的更多详细信息以及更多代码:Click- another question from StackOverflow

我需要做的是创建一个包含 CHotel 对象的文件并将它们插入到这个向量 m_hoteli 中。

至于为什么它不起作用,它要么没有从文件中读取字符串,要么根本没有填充向量。 这是我的文件:

“码头”5 500

“郁金香”4 400

“黑海”3 300

“瑞士钟”5 600

class CComplex:CHotel
{

protected:
    string m_complex;
    vector<CHotel> m_hoteli;
public:
    CComplex(){};

    CComplex(string filename, string nComplex)
    {


        fstream file("filename.txt", ios::in);
        CHotel temp(" ",0,0);
        while (file >> temp)
        {
            m_hoteli.push_back(temp);
        }
/* Second try:
m_complex = nComplex;

        fstream in;
        in.open(filename, ios::in);
        string s;
        while (getline(in, s))
        {
            CHotel h1(s);
            m_hoteli.push_back(h1);
    }

Third try:
m_complex = nComplex;
        ifstream iStream(filename);
        if (iStream.good())
        {
            copy(istream_iterator<CHotel>(iStream), istream_iterator<CHotel>(), back_inserter(m_hoteli));

            }
        }

*/


    }

这是 CHotel 代码:

class CHotel : public CTurist
{
protected:

    string hName;
    int stars;
    int beds;
    map<CTurist, unsigned> Turisti;

public:
    unsigned Sum = 0;
    int br = 0;
    CHotel(){};

    CHotel(string hName2, int zvezdi, int legla)
    {
        hName = hName;
        stars = zvezdi;
        beds = legla;
    }


    friend istream& operator>>(std::istream& is, CHotel& e)
    {
        is >> e.hName >> e.stars >> e.beds;;

        return is;
    }

我只是主要这样做:CComplex c1("filename.txt", "Complex1");

【问题讨论】:

  • 请注意,使用 istream>> 将逐行读取文件空间而不是逐行
  • @LorenceHernandez:啊,我明白了。这就是为什么逗号不起作用的原因。删除了所有逗号,现在它填充了向量。

标签: c++ class object vector


【解决方案1】:

您没有在 CComplex 构造函数中使用文件名参数,只是您的代码应该可以工作。

        fstream file(filename, ios::in);

你知道如何逐步调试吗? Here is some info on debugging

这是您的完整代码,唯一的变化是文件名参数,文件现在应该放在 c:\temp 中。

#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <fstream>

using namespace std;

class CTurist
{
protected:
    string tName;
    int age;

public:
    CTurist() {};

    CTurist(string name, int age2)
    {
        tName = name;
        age = age2;
    }


    bool operator<(const CTurist& e) const
    {
        return age < e.age;
    }

    friend ostream& operator<<(ostream& os, const CTurist&& e);
    friend ifstream& operator>>(ifstream& is, CTurist&& e);
};

class CHotel : public CTurist
{
protected:

    string hName;
    int stars;
    int beds;
    map<CTurist, unsigned> Turisti;

public:
    unsigned Sum = 0;
    int br = 0;
    CHotel() {};

    CHotel(string hName2, int zvezdi, int legla)
    {
        hName = hName;
        stars = zvezdi;
        beds = legla;
    }


    friend istream& operator>>(std::istream& is, CHotel& e)
    {
        is >> e.hName >> e.stars >> e.beds;;

        return is;
    }
};

class CComplex :CHotel
{

protected:
    string m_complex;
    vector<CHotel> m_hoteli;
public:
    CComplex() {};

    CComplex(string filename, string nComplex)
    {


        fstream file(filename, ios::in);
        CHotel temp(" ", 0, 0);
        while (file >> temp)
        {
            m_hoteli.push_back(temp);
        }
    }
};

int main()
{
    CComplex c1("C:\\temp\\file.txt", "Complex1");
    system("pause");
    return 0;
}

尝试在 main 中设置一个断点并使用 f11 和 f10 逐步执行您的程序

【讨论】:

  • 通过使用断点,fstream file(filename, ios::in);行给了我“读取字符串字符时出错”。第 226 行给出了这个: + file {_Filebuffer={_Set_eback=0x00000000 _Set_egptr=0x00000000 _Pcvt=0x00000000 ...} } std::basic_fstream > At最后,临时对象不会从 file.txt 中获取对象,因此不会用任何东西填充向量。
猜你喜欢
  • 2012-03-19
  • 2016-03-14
  • 2014-04-21
  • 2023-03-06
  • 1970-01-01
  • 1970-01-01
  • 2012-09-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多