mardi 28 avril 2015

In C++: How to read from file into a vector of objects

I would like to read data from a filename that the user specifies into a vector of objects. There are five different member variables per vector element that I would like to read in. In the file, there will multiple entries (groups of five member variables) that must be read into each vector element. Here is my (incomplete) code so far:

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;

However, there are several problems with this code already. One of which is that the setVIN(), setMake(), setModel(), setYear(), and setPrice member functions require one argument (the value in which to set VIN, Make, Model, etc). Here is the class declaration:

class Vehicle

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

        Vehicle(string, string, string, int, double);
        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);

Lastly, given the first block of code I posted, on the lines that have inputFile >> ..... an error message states "no operand '>>' matches these operands operand types are std::ifstream >> void"

Could anybody help me get through this road block?


Aucun commentaire:

Enregistrer un commentaire