C++ 提升序列化提升::archive_exception:输出流错误

C++ Boost Serialization boost::archive_exception: Output stream error

本文关键字:exception 输出流 错误 C++ 升序 序列化 archive      更新时间:2023-10-16


我正在使用 Boost 序列化库运行以下C++代码,该库首先序列化类 Info 的对象,然后将其检索回类 Info 的另一个对象:

#include <vector>
#include <fstream>
#include <boost/serialization/vector.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
class Info
{
private:
  // Allow serialization to access non-public data members.
  friend class boost::serialization::access;
  // Serialize the std::vector member of Info
  template<class Archive>
  void serialize(Archive & ar, const unsigned int version)
  {
    ar & filenames;
  }
  std::vector<std::string> filenames;
public:
  void AddFilename( const std::string& filename );
  void Print() const;
};
void Info::Print() const
{
  std::copy(filenames.begin(), filenames.end(), std::ostream_iterator<std::string>(std::cout, "n"));
}
void Info::AddFilename( const std::string& filename )
{
  filenames.push_back( filename );
}
int main(int argc, char** argv)
{
  Info info;
  info.AddFilename( "ThisFile.txt" );
  info.AddFilename( "ThatFile.txt" );
  info.AddFilename( "OtherFile.txt" );
   // Save filename data contained in Info object
  {
    // Create an output archive
    std::ofstream ofs( "store.dat" );
    boost::archive::text_oarchive ar(ofs);
    ar & info;
  }
  // Restore from saved data and print to verify contents
  Info restored_info;
  {
    // Create and input archive
    std::ifstream ifs( "store.dat" );
    boost::archive::text_iarchive ar(ifs);
    // Load the data
    ar & restored_info;
  }
  restored_info.Print();
  return 0;
}


我收到以下错误:terminate called after throwing an instance of 'boost::archive::archive_exception' what(): output stream error Aborted (core dumped)
如果有人能在这里帮助我,那就太好了。

谢谢。

添加LD_LIBRARY_PATH可以解决问题

LD_LIBRARY_PATH = pathtoboostlib export $LD_LIBRARY_PATH