145 / 42 / 24
Регистрация: 09.05.2022
Сообщений: 317
|
|
1
|
не работает десериализация. Что не так?
07.06.2022, 01:06. Показов 541. Ответов 2
Не понимаю, в чем ошибка. Почему программа ломается именно на десериализации класса institute? И как вообще сделать эту десериализацию?
main.cpp
C++ | 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
| Student s;
Lecturer l;
Course c;
Mark m;
Institute i;
ofstream fin123("student.txt");
ofstream finl("lecturer.txt");
ofstream finm("mark.txt");
ofstream finc("course.txt");
ofstream fini("institute.txt");
Serializer st,lec,mark,course,it;
st.SerializeStudent(fin123, s);
lec.SerializeLecturer(finl, l);
mark.SerializeMark(finm, m);
course.SerializeCourse(finc, c);
it.SerializeInstitute(fini, i);
ifstream fin_student("student.txt");
ifstream finl2("lecturer.txt");
ifstream finm2("mark.txt");
ifstream finc2("course.txt");
ifstream fini2("institute.txt");
s = st.DeserializeStudent(fin_student);
l = lec.DeserializeLecturer(finl2);
m = mark.DeserializeMark(finm2);
c = course.DeserializeCourse(finc2);
i = it.DeserializeInstitute(fini2); //вот здесь возникает проблема с памятью и ошибки |
|
institute.h
C++ | 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
| #include "Institute.h"
#include "Student.h"
#include "Lecturer.h"
#include <vector>
using namespace insti;
using namespace stud;
using namespace std;
void Institute::AddStudent(Student newStudent)
{
students.push_back(newStudent);
};
vector<Student>Institute::GetStudentsGroup(std::string groupName)
{
//Student s1;
for (int i = 0; i < students.size(); i++)
{
if (students[i].getGroupName() == groupName)
return students;
}
};
Student Institute::GetStudent(std::string studentNo)
{
Student s;
for (int i = 0; i < students.size(); i++)
{
if (students[i].getStudentNo() == studentNo)
s = students[i];
}return s;
};
bool Institute::DeleteStudent(std::string studentNo)
{
for (int i = 0; i < students.size(); i++)
{
if (students[i].getStudentNo() == studentNo)
students.erase(students.begin() + i);
return true;
}
};
void Institute::AddEmployee(Lecturer employee)
{
lecturers.push_back(employee);
};
Lecturer Institute::GetEmployee(int PersonnelNo)
{
Lecturer s;
for (int i = 0; i < lecturers.size(); i++)
{
if (lecturers[i].getPersonnelNo() == PersonnelNo)
s = lecturers[i];
}return s;
};
bool Institute::DeleteEmployee(int personnelNo)
{
for (int i = 0; i < lecturers.size(); i++)
{
if (lecturers[i].getPersonnelNo() == personnelNo)
lecturers.erase(lecturers.begin() + i);
}return true;
};
void Institute::AddCourse(Course course)
{
courses.push_back(course);
};
Course Institute::GetCourse(int id)
{
for (int i = 0; i < id; i++)
{
return courses[id];
}
};
bool Institute::DeleteCourse(int id)
{
courses.erase(courses.begin() + id);
return true;
};
void Institute::AddMark(Mark mark)
{
marks.push_back(mark);
};
vector<Mark> Institute::GetCourseMarks(int courseId)
{
vector<Mark>k;
for (int i = 0; i < marks.size(); i++)
{
if (marks[i].getCourseId() == courseId)
k.push_back(marks[i]);
}return k;
};
vector<Mark> Institute::GetStudentMarks(std::string studentNo)
{
vector<Mark>f;
for (int i = 0; i < marks.size(); i++)
{
if (marks[i].getStudentNo() == studentNo)
f.push_back(marks[i]);
}return f;
};
vector<Mark> Institute::GetGroupMarks(std::string groupName)
{
vector<Mark>f;
for (int i = 0; i < marks.size(); i++)
{
if (marks[i].getGroupName() == groupName)
f.push_back(marks[i]);
}return f;
};
vector<Student> Institute::getStudents()
{
Student a,b,c;
a.set_FIO("Нкиифоров");
students.push_back(a);
a.set_FIO("Сутаков");
students.push_back(a);
a.set_FIO("Чомчоев");
students.push_back(a);
students.push_back(b);
students.push_back(c);
//for(int i=0;i<students.size();i++)
return students;
};
vector<Lecturer> Institute::getLecturers()
{
Lecturer a,b,c;
a.setPersonnelNo(12);
a.setPosition("Преподаватель информатики");
a.set_Email("уцо@mail.com");
a.set_FIO("Лавреньтев Гарри Поттер");
b.setPersonnelNo(12);
b.setPosition("Преподаватель информатики");
b.set_Email("уцо@mail.com");
b.set_FIO("Лавреньтев Гарри Поттер");
c.setPersonnelNo(12);
c.setPosition("Преподаватель информатики");
c.set_Email("уцо@mail.com");
c.set_FIO("Лавреньтев Гарри Поттер");
lecturers.push_back(a);
lecturers.push_back(b);
lecturers.push_back(c);
return lecturers;
};
vector<Course> Institute::getCourses()
{
Course a, b, c;
Course s;
s.setSemester(5);
a.setLecturer(lecturers[0]);
a.setSemester(s.getSemester());
a.setSubject("программирование");
courses.push_back(a);
return courses;;
};
vector<Mark> Institute::getMarks()
{
Mark m;
m.setCourseId(22);
m.setGroupName("ФИИТ-29");
m.setMark(5);
m.setStudentNo("первый");
m.setYear(2000);
m.set_Email("hope@mail.ru");
m.set_FIO("Ведьма Из Блэр");
return marks;
}; |
|
serializer.h
C++ | 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
| #pragma once
#include<fstream>
#include<iostream>
#include"Person.h"
#include"Lecturer.h"
#include "Student.h"
#include <string>
#include"Mark.h"
#include"Course.h"
#include "Institute.h"
using namespace per;
using namespace lect;
using namespace stud;
using namespace std;
using namespace mar;
using namespace cours;
using namespace insti;
namespace seria
{
class Serializer
{
public:
void SerializePerson(std::ofstream& fileStream, Person person)
{
fileStream << "Person{\n" << person.get_FIO() << endl << person.get_Email() << endl;
};
Person DeserializePerson(std::ifstream& fileStream)
{
string s;
getline(fileStream, s);
Person person;
if (s.compare("Person{") == 0)
{
//fileStream >> s; //"{"
string n;
fileStream >> n;
person.set_FIO(n);
fileStream >> n;
person.set_Email(n);
return person;
}
};
void SerializeLecturer(std::ofstream& fileStream, Lecturer lecturer)
{
fileStream << "Lecturer{\n" <<"fio="<< lecturer.get_FIO() << endl<<"email=" << lecturer.get_Email() << endl <<"position="<< lecturer.getPosition() << endl << lecturer.getPersonnelNo() << "\n}";
};
Lecturer DeserializeLecturer(std::ifstream& fileStream)
{string e;
string s;
getline(fileStream, s);
Lecturer lecturer;
if (s.compare("Lecturer{") == 0)
{
string n;
getline(fileStream, n);
lecturer.set_FIO(n);
fileStream >> n;
lecturer.set_Email(n);
fileStream >> n;
lecturer.setPosition(n);
return lecturer;
}
};
void SerializeStudent(std::ofstream& fileStream, Student student)
{
SerializePerson(fileStream, student);
fileStream << student.getGroupName() << endl << student.getStudentNo() << endl << "}";
};
Student DeserializeStudent(std::ifstream& fileStream)
{
string s;
getline(fileStream, s);
Student student;
if (s.compare("Person{") == 0)
{
string n;
getline(fileStream, n);
getline(fileStream, n);
getline(fileStream, n);
student.setStudentNo(n);
getline(fileStream, n);
student.setGroupName(n);
return student;
}
};
void SerializeMark(std::ofstream& fileStream, Mark mark)
{
fileStream << "Mark{\n" << mark.getYear() << endl << mark.getStudentNo() << endl << mark.getCourseId() << endl << mark.getMark() << endl << "}";
};
Mark DeserializeMark(std::ifstream& fileStream)
{
string s;
getline(fileStream, s);
Mark mark;
if (s.compare("Mark{") == 0)
{
//fileStream >> s; //"{"
int n;
fileStream >> n; //превращение строки в объект (обратная операция сериализации, когда мы заносили объект в текстовый файл)
mark.setYear(n);
string s;
fileStream >> s;
mark.setStudentNo(s);
fileStream >> n;
mark.setCourseId(n);
fileStream >> n;
mark.setMark(n);
return mark;
}
};
Lecturer ler;
void SerializeCourse(std::ofstream& fileStream, Course course)
{
fileStream << "Course{\n" << course.getSubject() << endl << course.getSemester() << endl << course.getLecturer().getPersonnelNo() << endl << "}";
};
Course DeserializeCourse(std::ifstream& fileStream)
{
string s;
getline(fileStream, s);
Course course;
Lecturer l;
if (s.compare("Course{") == 0)
{
//fileStream >> s; //"{"
int n;
string s;
getline(fileStream, s);
course.setSubject(s);
fileStream >> n;
course.setSemester(n);
course.setLecturer(l);
return course;
}
};
void SerializeInstitute(std::ofstream& fileStream, Institute imi)
{
vector<stud::Student>student;
vector<Lecturer>lecturer;
vector<cours::Course>course;
vector<Mark>mark;
student = imi.getStudents();
lecturer = imi.getLecturers();
course = imi.getCourses();
mark = imi.getMarks();
fileStream << "Institute{\n";
for (int i = 0; i < student.size(); i++)
fileStream<<"Студент "<<i + 1 <<" " << student[i].get_FIO() << endl << endl;
//fileStream<<student[i];
for (int i = 0; i < lecturer.size(); i++)
fileStream << "Преподаватель: " << i + 1 << " " << lecturer[i].get_FIO() << endl << endl;
for (int i = 0; i < course.size(); i++)
fileStream << "Курс: " << i + 1 << " " << course[i].getSubject() << endl << endl;
for (int i = 0; i < mark.size(); i++)
fileStream << "Оценка: " << i+1 << " " << mark[i].getMark() << endl << endl;
fileStream << endl << "}";
}
Institute DeserializeInstitute(std::ifstream& fileStream)
{
string s;
//getline(fileStream, s);
Institute institute;
Student a;
if (s.compare("Institute") == 0)
{
return institute;
}
}
};
} |
|
0
|