segmentation fault

Ok, so I'm getting a segmentation fault when I try to solicit values for the member variables of a Roster object from the following code:

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#ifndef ROSTER_H
#define ROSTER_H
#define MAX_CAPACITY 10
#include "student.h"
#include <iostream>
using namespace std;
class Roster 
{
    public:
        Roster(string name, string code, string prof, int cred);
        // Roster constructor will set instance values based on the parameters
        
        Roster();
        // Default constructor will set instance variables to defaults

        bool addStudent(const Student& stud);
        // add a Student object to the Roster

        bool delStudent(const Student& stud);
        // delete a Student object from the Roster

        string getCourseName() const;
        // returns the course name of the Roster object

        string getCourseCode() const;
        // returns the course code of the Roster object

        string getInstructor() const;
        // returns the instructor name of the Roster object

        int getCredits() const;
        // returns the number of credits of the Roster object
        
        void setCourseName(string name);
        // sets the name of the course to 'name'

        void setCourseCode(string code);
        // set the course code to 'code'

        void setInstructor(string prof);
        // sets the instructor's name to 'prof'

        void setCredits(int cred);
        // sets the number of credits to 'cred'
		
	void outPut();
	// output all Students in the roster
	
	friend ostream& operator <<(ostream& os, const Roster r);
	// output all Students in the roster
	
	friend istream& operator >>(istream& is, Roster& r);
	// input a Roster

    private:
        string coursename;
        string coursecode;
        string instructor;
        int credits;
        Student list[MAX_CAPACITY];
        int lastindex;
		int search(const Student& stud);
        // this is needed for the add and delete student functions
		void sort();
};

#endif 


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
#include "student.h"
#include "roster.h"
#include <iostream>
using namespace std;

Roster::Roster(string name, string code, string prof, int cred) :
    coursename(name), coursecode(code), instructor(prof), credits(cred)
{
    lastindex = -1;
    // assign -1 to last index so that the first index filled will be 0
    
    Student s1;

}

Roster::Roster() :
	coursename("C++"), coursecode("CS211"), instructor("Alayev/Yang"), credits(3)
{
	lastindex = -1;
}

istream& operator >>(istream& is, Roster& r) {
	cout << "Course Name: ";
	is >> r.coursename;
	cout << "Course Code: ";
	is >> r.coursecode;
	cout << "Instructor: ";
	is >> r.instructor;
	cout << "Credits: ";
	is >> r.credits;
	return is;
}


1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include "Date.h"
#include "student.h"
#include "roster.h"
using namespace std;


int main() {
	Roster r;
	cin >> r;
	return 0;
}


It compiles fine, but when I run it I get

1
2
3
4
5
Course Name: C++
Course Code: 211
Instructor: Alayev
Credits: 3
Segmentation fault (core dumped)

I've given the entire roster.h file but not the entire roster.cpp file, b/c I'm pretty sure only the code shown has any bearing on the main.cpp file. Why would this be occurring?
Have you tried using a debugger to see what the variables look like when it crashes?
When I tried compiling it with the debugger like so

 
g++ -g -o main main.cpp roster.cpp student.cpp Date.cpp


and ran it through gdb i got the following back:

1
2
3
4
Program received signal SIGSEGV, Segmentation fault.
0x00000035e389d4d1 in std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string() () from /usr/lib64/libstdc++.so.6
Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.107.el6_4.5.x86_64 libgcc-4.4.7-3.el6.x86_64 libstdc++-4.4.7-3.el6.x86_64
(gdb)
however i cannot use the command as i don't have root access on the system (it's for a homework assignment)
Okay, so it turns out that

Segmentation fault (core dumped)

occurs simply from calling the Roster constructor (default or otherwise).
Can anyone spot a reason for this? I don't see what's wrong with my
object constructors...
This makes me suspicious:

9
10
    lastindex = -1;
    // assign -1 to last index so that the first index filled will be 0 


I would suggest you post more code.
Segmentation faults very often occur due to bad pointers.

A bad pointer is not pointing to a memory address that you may use, and when you try to use it sometimes it works, other times you get a Segmentation Fault.
Found the problem. In my Student class, there existed Date objects as member variables, but in neither the student.h file nor the roster.* files was "Date.h" included. Thanks for taking a look y'all, this was really frustrating but I'm glad the worst is over now :)
Topic archived. No new replies allowed.