#include <fstream>
#include <iostream>
#include <string>
using namespace std;
class country
{
private:
unsigned int residents;
unsigned long territory;
string hope;
string forma_pravleniya;
int cities;
int constituencies;
float GDP;
public:
country()
{
residents = territory = cities = constituencies = GDP = 0;
}
country(int r, long t, int cities, int con, float GDP, string hope, string forma)
{
this->residents = r;
this->territory = t;
this->cities = cities;
this->constituencies = con;
this->GDP = GDP;
this->hope = hope;
this->forma_pravleniya = forma;
}
void out()
{
cout << residents <<" жителей,\t" << territory << " площадь,\t" << cities << " городов,\t" << constituencies << " округов,\t" << GDP << " ВВП,\t" << hope << "\t" << forma_pravleniya;
}
};
int main(int argc, const char * argv[]) {
string fileName = "country.xml";
ofstream countries;
country Russia(144000000, 17125191, 1118, 8, 1.776, "Христианство", "Республика");
country Armenia(2968000, 29743, 48, 10, 0.1336, "Христианство", "Республика");
country USA(331900000, 9500000, 30000, 51, 25.34, "Протестантизм", "Либеральная демократия");
countries.open(fileName);
countries.write((char*)&Russia, sizeof(country));
countries.write((char*)&USA, sizeof(country));
countries.write((char*)&Armenia, sizeof(country));
countries.close();
ifstream countries_r;
countries_r.open(fileName);
country reading;
while(countries_r.read((char*)&reading, sizeof(country)))
{
reading.out();
cout << endl;
}
countries_r.close();
return 0;
}
Нужно сделать сериализацию и диссериализацию. Хочу вывести все, что в файлике, но после того, как он выведет все, выдает ошибку:
LR6(22997,0x1e3386500) malloc: *** error for object 0x600000c086c0: pointer being freed was not allocated
LR6(22997,0x1e3386500) malloc: *** set a breakpoint in malloc_error_break to debug