#ifndef ROSTER_H
#define ROSTER_H
#define MAX_CAPACITY 10
#include "student.h"
#include <iostream>
usingnamespace 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
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?
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)
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...
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 :)