繁体   English   中英

Boost序列化示例错误

[英]Boost serialization example error

我阅读了该示例,然后尝试运行以下代码: http : //www.boost.org/doc/libs/1_58_0/libs/serialization/doc/index.html

#include <fstream>

// include headers that implement a archive in simple text format
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>

/////////////////////////////////////////////////////////////
// gps coordinate
//
// illustrates serialization for a simple type
//

class gps_position {
private:
    friend class boost::serialization::access;
    // When the class Archive corresponds to an output archive, the
    // & operator is defined similar to <<.  Likewise, when the class Archive
    // is a type of input archive the & operator is defined similar to >>.

    template<class Archive>
    void serialize(Archive & ar, const unsigned int version) {
        ar & degrees;
        ar & minutes;
        ar & seconds;
    }
    int degrees;
    int minutes;
    float seconds;
public:

    gps_position() {
    };

    gps_position(int d, int m, float s) :
    degrees(d), minutes(m), seconds(s) {
    }
};

int main() {
    // create and open a character archive for output
    std::ofstream ofs("filename");

    // create class instance
    const gps_position g(35, 59, 24.567f);

    // save data to archive
    {
        boost::archive::text_oarchive oa(ofs);
        // write class instance to archive
        oa << g;
        // archive and stream closed when destructors are called
    }

    // ... some time later restore the class instance to its orginal state
    gps_position newg;
    {
        // create and open an archive for input
        std::ifstream ifs("filename");
        boost::archive::text_iarchive ia(ifs);
        // read class state from archive
        ia >> newg;
        // archive and stream closed when destructors are called
    }
    return 0;
}

我得到错误:

terminate called after throwing an instance of 'boost::archive::archive_exception'
  what():  output stream error

RUN FINISHED; Aborted; core dumped; real time: 100ms; user: 0ms; system: 0ms

我只是将代码复制粘贴,编译,运行,没有问题。 基于此和您的错误消息,我猜您有系统问题。 可能是某些原因阻止您编写文件。 最可能的罪魁祸首是权限错误。 如果您正在写一个大文件,我可能会猜到磁盘已满,但似乎不太可能已经满了。

尝试在其他位置运行该可执行文件,或对您具有写许可权的位置进行硬编码。 使用ofstream尝试一个简单的世界,看看是否可以创建和写入文件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM