Jun 7, 2018 at 2:21pm UTC
I am trying to make a simple "person" vector of objects which can be saved and then loaded from real file to vector of objects. I made a function found in some tutorials which returns all the content from class, but I have no clue what to do with it, to put it into a file. Should I use i/o stream operators or something? At now I have the following code:
#include <iostream> // person.h
#include <string>
#include <vector>
using namespace std;
#include "rapidjson/writer.h"
#include "rapidjson/stringbuffer.h"
using namespace rapidjson;
class person {
public:
std::string name;
string surname;
int age;
person();
~person();
person(string, string, int);
template <typename Writer>
void Serialize(Writer& writer) const {
writer.StartObject();
writer.String("name");
writer.String(name.c_str());
writer.String("surname");
writer.String(surname.c_str());
writer.String(("id"));
writer.Uint(age);
writer.EndObject();
}
std::string serialize(){
StringBuffer s;
Writer<StringBuffer> writer(s);
Serialize(writer);
return s.GetString();
}
};
#include "person.h" // person.cpp
person::person() {
}
person::~person() {
}
person::person(string name, string surname, int age) : name(name), surname(surname), age(age) {
}
#include "person.h" // main.cpp
int main() {
vector<person> Save;
person P1("Tak", "Nie", 20);
person P2("Yes", "No", 10);
Save.push_back(P1);
Save.push_back(P2);
cout << P1.serialize();
cout << P2.serialize();
return 0;
}
Last edited on Jun 7, 2018 at 2:22pm UTC