int main(){
long col, ghost, x, y, d;
string command;
cout << "read file" << endl;
ifstream ifs("ghostleg.txt");
if(ifs.fail()){
cerr << "Failed to open ghostleg.txt" << endl;
exit(1);
}
ifs >> col;
Link *player = new Link[col];
for(long i=0; i<col; i++){
init(player[i]);
}
ifs >> ghost;
for(long i=0; i<ghost; i++){
ifs >> x;
ifs >> y;
ifs >> d;
insert(player, x-1, y-1, d, col);
}
return 0;
}
However, when I run the program, after it calls the insert function, calls the search function, and reaches the statement "tmp = l->next;", it seg faults (according to debugger). I've been thinking for hours but I can't seem to see why it seg faults. Could somebody please help me?
Not a scooby. I can't test your code myself because it's missing the preceed function. When you're causing a segFault and you're doing it with a pointer, that's how it's done; NULL dereference or memory that isn't yours.
You are passing the Link to the init() by value so the assignment l = new node; does not change player[i] on line 16 in the main().
Try passing by reference. Change the init() to: void init(Link& l).
That should get you past the 1st problem, though I see others waiting for you to discover.