C++ programming: Segmentation fault reading object pointers from a vector?
-
I've been trying to get a program running for a class I'm in and I've been stuck on the same spot forever! I get a segmentation fault from the last line of the following code - Does anyone know why? I suppose I should also be adding "delete ap;" at the end of the while loop according to my instructor but that's giving me even more trouble on a separate program. Forgive me if it's an easy fix, its only my first semester in programming. Thanks! #include <iostream> #include <vector> #include <fstream> #include <string> using namespace std; class Athlete { public: Athlete(string, string, string, string, string); void display(); private: string jersey_number; string best_time; string sport; string name; string high_school; }; Athlete::Athlete(string name2, string jersey_number2, string best_time2, string sport2, string high_school2) : name(name2), jersey_number(jersey_number2), best_time(best_time2), sport(sport2), high_school(high_school2) { } void Athlete::display() { cout << name; } int main() { string title; ifstream stream; stream.open("data.txt"); string name; vector<Athlete*> catalog; while(getline(stream, name, '|').eof()) { Athlete* ap; string jersey_number; string best_time; string sport; string high_school; getline(stream, jersey_number, '|'); getline(stream, best_time, '|'); getline(stream, sport, '|'); getline(stream, high_school, '|'); ap = new Athlete(name, jersey_number, best_time, sport, high_school); catalog.push_back(ap); } stream.close(); Athlete* athlete; athlete = catalog[1]; athlete->display(); } The "data.txt" file is a pipe delimited file containing: (generally contains some integers but I changed them to strings to make it easier) John|eighty|fifty swimming|Jefferson Jane|twenty|ten sprinting|San Marin
-
Answer:
The problem is that there are no elements in your vector, and you are calling the display function on the second one. The reason there are no elements in your vector is because your loop condition is messed up. while (getline(stream, name, '|').eof()) eof() is going to return false if you haven't reached the end of file. On your first read, you clearly would not have reached the end of the file, so the loop is never entered. This is how I would do that loop: while (true) { string name, jersey_number, best_time, sport, high_school; getline(stream, name, '|'); getline(stream, jersey_number, '|'); getline(stream, best_time, '|'); getline(stream, sport, '|'); getline(stream, high_school, '|'); if (!stream) break; Athlete * ap = new Athlete(name, jersey_number, best_time, sport, high_school); catalog.push_back(ap); } Although, as you have your data file now, this will not work quite right, because your data file is not quite pipe delimited like you say. It is a combination of newline and pipe delimited. You could either fix your data file to be actually pipe delimited, or you could modify the code I gave you so that the getline called for best time and for high school uses '\n' as the delimiter instead of '|'. And no, you shouldn't be adding 'delete ap' at the end of the loop. You're creating objects dynamically, and putting pointers to those objects in the vector so ap and the pointer in the vector both point to the same object. If you call 'delete ap', that's going to destroy the object, leaving your vector with dangling pointers. Why do you have a vector of pointers instead of simply vector<Athlete>? Is that an assignment requirement? Anyway, to properly clean up your objects, you need to add a loop at the end of main, after you're done with the objects, that calls delete on each pointer in the vector.
john at Yahoo! Answers Visit the source
Related Q & A:
- What could be the possible reasons for segmentation fault?Best solution by Stack Overflow
- What are some basic c++ programming techniques?Best solution by Quora
- What certification can I get for C Programming?Best solution by Yahoo! Answers
- How hard is C++ programming?Best solution by Yahoo! Answers
- What is a Vector Image?Best solution by wiki.answers.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.