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


i read data filename user specifies vector of objects. there 5 different member variables per vector element read in. in file, there multiple entries (groups of 5 member variables) must read each vector element. here (incomplete) code far:

while (!inputfile.eof())             {                 (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 several problems code already. 1 of setvin(), setmake(), setmodel(), setyear(), , setprice member functions require 1 argument (the value in set vin, make, model, etc). here class declaration:

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

lastly, given first block of code posted, on lines have inputfile >> ..... error message states "no operand '>>' matches these operands operand types std::ifstream >> void"

could me through road block?

thanks!

firstly code bad.

inputfile >> temp[count].getvin(); 

it gets string getvin() , tries read temporary string. instead need use like:

string vin; inputfile >> vin; temp[count].setvin(vin);  

secondly more ideomatic create operator>> reads whole object loops can cleaner.

istream& operator>>(istream& is, vehicle & v) {    string vin;    inputfile >> vin;    v.setvin(vin);    ... } 

and if make member function can instead write

// want add error checking each read void vehicle::readfromstream(istream & is) {    is>>vin;    is>>make;    ... }   istream& operator>>(istream& is, vehicle & v) {    v.readfromstream(is);    return is; } 

then loop become

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

so long add sensible operator<< (in same way.)

if want store them in list then:

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

Comments

Popular posts from this blog

java - Spring Data JPA: Why findOne(id) executing delete query internally? -

python - Mongodb How to add addtional information when aggregating? -

java - Incorrect order of records in M-M relationship in hibernate -