0

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;
}
  • Boost is a red herring. `get_username` is private. It can only be seen by friends and the class itself. Move the method to the public section if the class definition. – Niall Feb 26 '17 at 20:02
  • A general note on the intelligence error you see. Whilst the intelligence has improved dramatically, running the code through an actual compile will give you much better error messages and is a better idea than just relying on the intellisense marks. – Niall Feb 26 '17 at 20:08
  • After moving `get_username()` to the public area of the class. The previous error has disappeared however i am now getting a linker error **LNK2001 unresolved external symbol** for `test2.get_username()` –  Feb 26 '17 at 20:21
  • You have to implement the method; http://stackoverflow.com/q/12573816/3747990 – Niall Feb 27 '17 at 06:33
  • the method is implemented in a separate cpp file `User.cpp` –  Feb 27 '17 at 20:09

0 Answers0