c++ - Windows 中 boost::serialization 的未知异常

标签 c++ exception boost-serialization

这是我尝试过的方法,但是我得到了 Error: Unknown exception thrown

try{//load
  std::ifstream stream(arch_name.c_str());
  std::cout << ">> " << "Started deserializing " << arch_name << std::endl;
  boost::archive::binary_iarchive arc(stream);
  arc & c & matrix;
  stream.close();
  std::cout << ">> " << "Finished deserializing" << std::endl;
}catch(std::exception e){
  std::cout << "Error: " << e.what() << std::endl;
  return 1;
}

这在带有 gcc 的 Linux 中运行良好。我在 Windows 中使用 Visual Studio。

编辑

回溯显示 basic_binary_iprimitive.hpp 正在抛出异常

template<class Archive, class Elem, class Tr>
inline void basic_binary_iprimitive<Archive, Elem, Tr>::load_binary( void *address,     std::size_t count){
//.....
if(scount != s)
    boost::serialization::throw_exception(
        archive_exception(archive_exception::input_stream_error)
    );

编辑

我将 catch black 更改为 catch(boost::archive::archive_exception e) 并打印出来。 输入流错误

boost archive exception 中的 virtual what 有没有遗漏?

最佳答案

我遇到这个问题是因为我遇到了同样的问题 - Windows 上的 boost 序列化异常。我在 Greg 的评论中找到了解决方案:在创建数据流时定义 i/o 模式。

我改变了这个:

std::ofstream ofs(filename);
boost::archive::binary_oarchive ar(ofs);

对此:

std::ofstream ofs(filename, std::ios::binary);
boost::archive::binary_oarchive ar(ofs);

然后在反序列化的时候就没有再出现异常了!

我认为这个问题需要一个实际的答案(而不仅仅是评论),所以我自己添加了它,但所有功劳归于 Greg。

关于c++ - Windows 中 boost::serialization 的未知异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11924126/

相关文章:

java - BoxLayout can't be shared error while setting it for JPanel 如何解决?

c++ - 反序列化没有默认构造函数的类型的STL容器

c++ - 反序列化构造函数层次结构

c++ - Boost 二进制序列化有时不起作用。解析的数据有时会损坏

c++ - 如何使用具有不同模板参数的变量制作 vector ?

c++ - 当使用 pcl.1.8.0 显示一些示例时,但在运行 "triangulationGreedyProjection "的代码时出错并在 “Memory.h” 崩溃

c# - 如何在自定义方法中处理错误的参数值?

java - Java 中的错误类

c++ - 如何访问继承成员

c++ - boost 序列化省略包装器的版本