Segmentation fault (core dumped)

Hi everyone :)
i'm writing a scheduling programm and i can't solve this problem: when i run the input driver(which is suppose to initialize a vector of objects with the information contained in a file), it gives me a Segmentation fault(core dumped error) and i don't know why.

The class i've defined is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class MachineFamily
{
	friend istream& operator>>(istream& is, MachineFamily& f);
	friend ostream& operator<<(ostream& os, const MachineFamily& f);
	
 public:
	MachineFamily(){};
	MachineFamily(const MachineFamily& f);
	
	string Name() const {return name;}
	float DMin() const {return min_diameter;}
	float DMax() const {return max_diameter;}
	
	unsigned Machine(unsigned i) const {return machines[i];}
	unsigned NumMachines() const {return num_machines;}
	int SearchMachine(unsigned m) const;
	
	int TipsIndicator() const {return tips_indicator;}
	string Tip(unsigned i) const {return tips[i];}
	unsigned NumAllowedTips() const {return tips.size();}
	bool TipAllowed(string c) const;	
	
 private:
	string name;
	float min_diameter, max_diameter;
	
	unsigned num_machines;
	vector<unsigned> machines;
	
	int tips_indicator;					
	vector<string> tips; 				
	
};

and the operator >> is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
istream& operator>>(istream& is, MachineFamily& f)
{
	int i = 0, j = 0;
	unsigned m;
	string t;
	is >> f.name >> f.min_diameter >> f.max_diameter >> f.num_machines;
	
	do
	{
		is >> m;
		f.machines.push_back(m);
		i++;
	}
	while(i != (int)f.num_machines);
	
	is >> f.tips_indicator;
	if(f.tips_indicator > 0)
	{
		do
		{
			is >> t;
			f.tips.push_back(t);
			j++;
		}
		while(j != f.tips_indicator);
	}
	return is;
}

I wanna create a vector<MachineFamiliy> which is initialized by this file.txt:

9
GL 203.2 304.8 13 1720 3020 3021 3023 3024 3244 3311 3312 3329 3349 3350 3385 3386 0
GLSMALL 100 304.8 2 3022 3245 0
GL2000 127 406.4 17 3438 3439 3440 3441 3476 3477 3478 3523 3524 3527 3530 3540 3541 3544 3578 3580 3581 0
GL2000SMALL 80 406.4 3 3570 3579 3582 0 
GLX 165 450 5 4063 4064 4065 4066 4067 0 
GLL 180 450 26 3817 3863 3864 3865 3874 3877 3880 3894 3895 3896 3897 3910 3911 3928 3929 3930 3931 3932 4056 4057 4058 4060 4061 4080 4081 4082 0
GLLCONCAVE 180 450 1 3927 -1
GLLBIG 180 800 3 3866 4062 4079 0 
SPECIAL 150 400 4 4111 4112 4113 4114 0 

The input driver is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include ....

int main()
{
        vector<MachineFamily> mf;
	int num_families;
	MachineFamily f;
	
	ifstream is ("families.txt");
	
	is >> num_families;
	
	for (int i = 0; i < num_families; i++)
	{
		is >> f;
		mf.push_back(f);
	}

	is.close();
        
        return 0;
}

i don't know what to do.. i think the problem id in operator>> but i don't know how to solve it.

Hope i've explained myself well...
Can someone help me please?


Last edited on
I did not get seg fault, but think that the reading was off.
The size of the MachineFamily::machines vector did not correspond with the MachineFamily::num_machines.
The vector was increasing from the previous readings, you need to clear it first.
Ditto for `tips'

Also, limit the scope
1
2
3
4
5
6
	for (int i = 0; i < num_families; i++)
	{
		MachineFamily f;
		is >> f;
		mf.push_back(f);
	}
I've tried to do what you told me but it didn't work..

I'm usign cygwin to compile and run the program. Could the problem be there?
Thank you for your help!! :)
I've found the problem!! It's the copy constructor MachineFamily(const MachineFamily& f). It's enough to remove it and I don't have the error msg anymore!!!
Topic archived. No new replies allowed.