vector<Flight> flights;
while (!myReadFile.eof()) {
flights.push_back(*(new Flight()));
// read some info...
}
after the second loop the program crashes with the message:
“Unhandled exception at 0x776315de in cpi.exe: 0xC0000005: Access violation reading location 0xfeeefee2.”
How can I solve the problem?
edit:
vector<Flight> flights;
while (!myReadFile.eof()) {
flights.push_back(Flight());
// read some info...
}
i tried this and still crash on the second loop
edit: full while
int count = 0;
myReadFile >> output;
while (!myReadFile.eof()) {
flights.push_back(Flight());
flights[count].setFlightNum(atoi(output));
myReadFile >> output;
int x = atoi(output);
flights[count].setStartX(x);
myReadFile >> output;
int y = atoi(output);
flights[count].setStartY(y);
count++;
myReadFile >> output;
}
You should stream directly into ints. If you have to read “tokens” then use a std::string but reading into a char array is always dangerous.
You should also probably have code that will create a Flight object from a stream, albeit that I dislike the use of
std::istream& operator>>(std::istream&, Flight& ), I find it “intrusive” and non-extensible. I prefer factories for this. However let’s write that function anyway:And now: