【问题标题】:In C++: How to read from file into a vector of objects在 C++ 中:如何从文件中读取对象向量
【发布时间】:2015-04-29 00:57:08
【问题描述】:

我想从用户指定的文件名中读取数据到对象向量中。我想读入的每个向量元素有五个不同的成员变量。在文件中,必须将多个条目(五个成员变量的组)读入每个向量元素。到目前为止,这是我的(不完整的)代码:

while (!inputFile.eof())
            {
                for (unsigned int count = 0; inputFile.eof(); count++)
                {
                    cout << "Vehicle #" << (count + 1) << endl;

                    inputFile >> temp[count].setVIN();                              
                    cout << "VIN: " << temp[count].getVIN() << endl;            
                    inputFile >> temp[count].setMake() << endl;                 
                    cout << "Make: " << temp[count].getMake() << endl;          
                    inputFile >> temp[count].setModel() << endl;                
                    cout << "Model: " << temp[count].getModel() << endl;        
                    inputFile >> temp[count].setYear() << endl;                 
                    cout << "Year: " << temp[count].getYear() << endl;          
                    inputFile >> temp[count].setPrice() << endl;                
                    cout << "Price: " << temp[count].getPrice() << endl         
                         << endl;
                }
            }

但是,此代码已经存在几个问题。其中之一是setVIN()setMake()setModel()setYear()setPrice 成员函数需要一个参数(用于设置 VIN、品牌、型号等的值)。这是类声明:

class Vehicle
{
    private:

        string VIN;
        string make;
        string model;
        int    year;
        double price;

    public:
        Vehicle(string, string, string, int, double);
        Vehicle();
        string getVIN();
        string getMake();
        string getModel();
        int    getYear();
        double getPrice();
        void   setVIN(string);
        void   setMake(string);
        void   setModel(string);
        void   setYear(int);
        void   setPrice(double);
 };

最后,给定我发布的第一个代码块,在具有inputFile &gt;&gt; ..... 的行上,一条错误消息指出“没有操作数 '>>' 与这些操作数匹配,这些操作数类型是 std::ifstream >> void”

谁能帮我度过这个路障?

谢谢!

【问题讨论】:

  • 另外,inputFile &gt;&gt; temp[count].setVIN(); 应该如何工作?实际签名是void setVIN(string);,它需要std::string 作为参数,然后返回void。这绝不是调用此函数的合适语法。
  • 创建一个读取一个对象的函数,如果读取成功,则将该对象放入向量中。
  • 粘贴输入文件的示例会有所帮助

标签: c++ file class object vector


【解决方案1】:

首先这段代码不好。

inputFile >> temp[count].getVIN();

它从getVIN() 获取一个字符串,然后尝试读入临时字符串。你需要使用类似的东西:

string vin;
inputFile >> vin;
temp[count].setVin(vin); 

其次,创建一个读取整个对象的operator&gt;&gt; 更符合理念,这样您的循环就可以更清晰。

istream& operator>>(istream& is, Vehicle & v) {
   string vin;
   inputFile >> vin;
   v.setVin(vin);
   ...
}

如果你把它设为成员函数,你可以改为写

// You probably want to add error checking to each read too
void Vehicle::readFromStream(istream & is) {
   is>>vin;
   is>>make;
   ...
}


istream& operator>>(istream& is, Vehicle & v) {
   v.readFromStream(is);
   return is;
}

那么你的循环就会变成

Vechicle v;
while(input>>v) {
   std::cout<<v<<std::endl;
}

只要您也添加一个明智的operator&lt;&lt;(以相同的方式)。

如果您真的想将它们存储在列表中,那么:

std::vector<Vehicle> vehicles;
Vehicle v;
while(input>>v) {
  vehicles.push_back(v);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-05
    • 1970-01-01
    • 1970-01-01
    • 2012-10-25
    • 1970-01-01
    • 1970-01-01
    • 2021-06-19
    相关资源
    最近更新 更多