Dec 8, 2010 at 11:20am UTC
I get segmentation fault with this code:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include "Class_Equipment.h"
#include "explode.h"
using namespace std;
vector<equipment> equipment_vector;
void load_equipment()
{
string line;
ifstream equipment_input ("equipment.txt");
while (equipment_input.good() )// is true if equipment_input isn't empty
{ getline(equipment_input,line);
string *this_line_array = explode (line, "\t");
equipment_vector.push_back(equipment(this_line_array[0],this_line_array[1],
this_line_array[2],this_line_array[3],this_line_array[4],this_line_array[5]));
}
equipment_input.close();
}
int main()
{
load_equipment();
}
the class equipment is very strait forward: it holds some things you can give to the class when you make it.
the explode function "explodes" a string, meaning it sets all the words witch are separated by "\t"(tab) in a array.
I hope you guys can figure it out I have been looking for hours
Dec 8, 2010 at 12:05pm UTC
Almost certainly one or more of: this_line_array[0]
, this_line_array[2]
, this_line_array[3]
, this_line_array[4]
and this_line_array[5]
is NULL.
You may want to revise the interface to explode
. If it parses an equipment
from a string
, why not make it return an equipment
?
Last edited on Dec 8, 2010 at 12:07pm UTC
Dec 8, 2010 at 3:34pm UTC
can you put the Class_Equipment.h
and explode.h
files up please? and I agree with kbw if you have 5 items in an array you might want to try to set the array to 6 if you haven't already done so.
Dec 13, 2010 at 2:46pm UTC
It was indeed what kbw was thinking off, thx for the help guys!