1

I am having trouble with a program in which I am using dynamic string arrays using pointers. After I am done printing out the array's elements in the function courseLoad, I get a Segmentation Fault(Core Dumped) error. Here is my code:

5 #include<iostream>
6 #include<cstdlib>
8
9 using namespace std;
10
11 class Student
12 {
13     public:
14         string name;
15         int numClasses;
16         string* classList;
17
18   Student() {};
19     Student(string, int, string*);
20     ~Student() {delete[] classList;};
21
22     string getName();
23     void setName();
24     int getNumClasses();
25     void setNumClasses();
26     string* getClassList();
27     void setClassList();
28
29     void inputs();
30     void courseLoad();
31     void emptyClasses();
32
33     Student operator=(const Student& other);
34
35 };
36
37 Student::Student(string name, int numClasses, string* classList)
38 {
39     this->name = name;
40     this->numClasses = numClasses;
41     classList = new std::string[100] ();
42 }
43
44 string Student::getName()
45 {
46     return name;
47 }
48
49 void Student::setName()
50 {
51     cout << "Please type the name of the student: ";
52     cin >> name;
53 }
54
55 int Student::getNumClasses()
56 {
57     return numClasses;
58 }
59
60 void Student::setNumClasses()
61 {
62     cout << "Please enter the amount of classes this student is taking: ";
63     cin >> numClasses;
64 }
65
66 string* Student::getClassList()
67 {
68     return classList;
69 }
70
71 void Student::setClassList()
72 {
73     cout << "Please enter the classes that this student is taking: ";
74     for(int i = 0; i < numClasses; i++)
75     {
76         getline(cin, classList[i]);
77     }
78 }
79
80 void Student::inputs()
81 {
82   string word;
83   classList = new string[100];
84
85     cout << "Please enter the name of the student: ";
86     getline(cin, name);
87     cout << "Please enter the number of classes that " << name << " is taking: ";
88     cin >> numClasses;
89     cin.ignore();
90
91     cout << "Please enter the names of the classes " << name << " is taking: ";
92     for(int i = 0; i < numClasses; i++)
93     {
94       getline(cin, word);
95       classList[i] = word;
96     }
97 }
98
99  void Student::courseLoad()
100 {
101     cout << name << endl;
102     cout << "===============================" << endl;
103     for(int i = 0; i < numClasses; i++)
104     {
105       cout << classList[i];
106       cout << "\n";
107     }
108 }
109
110 void Student:: emptyClasses()
111 {
112     numClasses = 0;
113     for(int i = 0; i < numClasses; i++)
114     {
115         classList[i] = "";
116     }
117 }
118
119 Student Student::operator=(const Student& other)
120 {
121   Student temp;
122   return (temp);
123 }
124
125 int main()
126 {
127     Student s1, s2;
128
129     s1.inputs();
130     cout << "Student 1's Data: ";
131     s1.courseLoad();
132     cout << "Hello";
133
134     s2 = s1;
135     cout << "Student 2's data after assignment from student 1: " << endl;
136     s2.courseLoad();
137
138     s1.emptyClasses();
139     cout << "Student 1's data after reset: " << endl;
140     s1.courseLoad();
141
142     cout << "Student 2's data, should still have original classes: " << endl;
143     s2.courseLoad();
144
145     return 0;
146 }
Weava
  • 496
  • 2
  • 13
  • Before going any further, you should consider dropping `numClasses` and `classList` and use a `std::vector`. – diapir Nov 03 '13 at 01:51
  • I wanted to avoid using std::vector but I could not figure out what was wrong so I resorted to using it. The program works now, thank you for your help! – Weava Nov 03 '13 at 02:23
  • I can't imagine why you would want to use anything else for this project *besides* `std::vector`If its because you think dynamic allocation is "better", rest assured that is already covered for you by `std::vector<>`. This really is the better way. – WhozCraig Nov 03 '13 at 02:28

1 Answers1

0
Student Student::operator=(const Student& other)
{
    Student temp;
    return (temp);
}

The expectation of the above member function is to do a member vise copy from other to the current object. In your case, assignment operation is doing nothing useful. When you said -

s2 = s1;

None of the s1 contents are really getting copied to s2. So, calling s2.courseLoad(); yields unpredicted results. For more information, read What is The Rule of Three ?

Community
  • 1
  • 1
Mahesh
  • 34,573
  • 20
  • 89
  • 115