Search code examples
classoopinheritancesegmentation-faultheader-files

Segmentation fault in header file


I have defined a Parent.h file, having Parent class with following data members:-

#pragma once
#include <string>
using namespace std;
class Person
{
private:
    string id;
    string name;
    string email;
    int contact_number;
    string address;
public:
    Person();
    ~Person();
    void set_id(string);
    void set_name(string);
    void set_email(string);
    void set_contact_number(int);
    void set_address(string);
    string get_id();
    string get_name();
    string get_email();
    int get_contact_number();
    string get_address();
};
Person::Person()
{
    this->id = "";
    this->name = "";
    this->email = "";
    this->contact_number = 0;
    this->address = "";
}
void Person::set_id(string id)
{
    this->id =id;
}

I have defined the rest of the functions(setters and getters) likewise in Parent.h file.

After that I am making a child class in Student.h header file, this Student class will publicly inherit the Parent class

#pragma once
#include"Courses.h" //course is aggregating to a student
#include"AcademicRecord.h"
#include "Person.h"
#include "Department.h"
#include<string>
#include<vector>
using namespace std;
class Student:public Person,public AcademicRecord,public Department
{
private:
    
    string institute_email;

public:
    Courses A;//A contains all information about the courses a student has opt for.
    Student();
    ~Student();
    //Using polymorphism between methods
    void ShowStudentAcademicRecord(); //shows the academic record of current student
    void ShowStudentCoursesInfo(); //getting the information for all courses of student
    
    //setter
    void set_institute_email(string email);
    void setDepartmentID(string);

    //getter
    string get_institute_email();

};

After that I have a Display.h file having vector of Students as its Data member and using a member function in Display, I am asking user to input data members like ID,Name,Address,... for individual students.

#include "Student.h"
using namespace std;
class Display
{
public:
    vector<Student> students;
Display();//default constructor
//setter
    void setStudentData();
};
void Display::setStudentData()
{
    int size;
    cout<<"Enter the number of students: ";
    cin>>size;
Student *current; //current Student
    for (int i = 0; i < size; i++)
    {
    current = &students[i];
    cout<<"For student "<<i+1<<"\nEnter the following: \n";//For general details
    cout<<"ID  Name  Email  Contact_Number  Address Institute_Email\n";
    string ID,Name,EMail,Address,insti_email;
    int contact_number;
    cin>>ID>>Name>>EMail>>contact_number>>Address>>insti_email;
    current->set_id(ID);
    current->set_name(Name);
    current->set_email(EMail);
    current->set_contact_number(contact_number);
    current->set_address(Address);
    current->set_institute_email(insti_email);
}

And when I try inserting value insides students by executing main.cpp file. I get segmentation fault at run-time inside Person class at setter function, where the first setter I have declared inside the class is void set_id(string).

#include "Display.h"
#include<vector>
#include<string>
int main()
{
Display display;
display.setStudentData();
}

Error:- Screenshot showing segmentation Error at this->id = id of set_id(string) function of Person class

I tried:- 1.Rechecking the setter function for any error 2.Bring all the classes inside the main.cpp file,instead of header files, but still the error continues.

I expected the program to take the desired input for various data members of student elments of vector students inside Display class. Instead I got a seg-fault.

Please if anyone can tell me what I have done wrong, or do I have to do malloc somewhere, inside a function?


Solution

  • As Nate Eldredge, has said I should use push_back instead of accessing ith element of students vector in setStudentData() function, as initially during default declaration of a Display class, the students vector is empty. Hence trying to access students[i] in current = &students[i], I am getting segmentation fault.

    The appropriate code for Display.h should be then

    #include "Student.h"
    using namespace std;
    class Display
    {
    public:
        vector<Student> students;
    Display();//default constructor
    //setter
        void setStudentData();
    };
    void Display::setStudentData()
    {
        int size;
        cout<<"Enter the number of students: ";
        cin>>size;
        total_students += size;
        Student *temp =new Student(); //temp Student
        for (int i = 0; i < size; i++)
        {
        cout<<"For student "<<i+1<<"\nEnter the following: \n";//For general details
        cout<<"ID  Name  Email  Contact_Number  Address Faculty_Type  Faculty_Description\n";
        string ID,Name,EMail,Address,insti_email;
        int contact_number;
        cin>>ID>>Name>>EMail>>contact_number>>Address>>insti_email;
        temp->set_id(ID);
        temp->set_name(Name);
        temp->set_email(EMail);
        temp->set_contact_number(contact_number);
        temp->set_address(Address);
        temp->set_institute_email(insti_email);
    
        cout<<"Enter the Course Details: \n";//For Course Details
        int size_course;
        cout<<"Enter the number of courses: ";
        cin>>size_course;
        cout<<"Enter the following for each course: \n";
        cout<<"Course_Name  Marks  Attendance Percentage \n";
        string name;
        int marks;
        float attendance;
        for (int i = 0; i < size_course; i++)
        {
            cin>>name>>marks>>attendance;
            temp->A.set_courses_enrolled(name);
            temp->A.set_course_wise_marks(marks);
            temp->A.set_coursewise_attendance_percentage(attendance);
        }
        cout<<"Enter the Academic Record Details: \n";//For Academic Record Details
        string program_name; int admission_no, enroll_no, begin_year, end_year, credits; float CGPA;
        cout<<"Enter Program_name, admission_no enroll_no begin_year end_year credits CGPA";
        cin>>program_name>>admission_no>>enroll_no>>begin_year>>end_year>>credits>>CGPA;
        setStudentAcademicRecord(temp,i,program_name,admission_no,enroll_no,begin_year,end_year,credits,CGPA); //passing temp pointer in this one
    
        students.push_back(*temp); //adding a new student in students vector
        }