I am trying to store a User object and then read the stored object using the boost serialization library and VS2015 community. I followed the tutorial here. Currently the read/write object to file works just fine; however, after reading the object back in I cannot access any of the objects members (i.e. username and pw_hash). Is there something I am missing? I have seen tons of questions how to write/read the object using the library but none that show anyone accessing the objects members after reading the object from file.
Class:
#ifndef USER_H
#define USER_H
#include <fstream>
#include <string>
#include <boost\serialization\string.hpp>
#include <boost\archive\text_oarchive.hpp>
#include <boost\archive\text_iarchive.hpp>
#include "Hash.h"
class User
{
public:
User();
User(std::string & name, std::string & pwd);
~User();
private:
std::string pw_hash;
std::string username;
inline std::string get_username();
inline void set_username(const std::string & name);
inline std::string get_PwHash();
inline void set_PwHash(const std::string & hash);
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & username;
ar & pw_hash;
}
};
#endif
Below is where I encounter my problem. After the object is read from memory, VS2015 underlines test2.get_username()
saying it is inaccessible.
Implementation:
#include "User.h"
int main(int argc, char *argv[])
{
User test("User1", "Password");
std::cout << test.get_username() << std::endl;
{
std::ofstream ofs("User1");
boost::archive::text_oarchive oa(ofs);
oa << test;
}
User test2();
{
std::ifstream ifs(username);
boost::archive::text_iarchive ia(ifs);
ia >> test2;
//error
std::cout << test2.get_username() << std::endl;
}
return 0;
}