- 论坛徽章:
- 0
|
- #include <boost/archive/text_oarchive.hpp>
- #include <boost/archive/text_iarchive.hpp>
- #include <fstream>
- struct wxPoint {
- int x;
- int y;
- };
- namespace boost {
- namespace serialization {
- template<class Archive>
- void serialize(Archive &ar, wxPoint p, unsigned int version){
- ar & p.x;
- ar & p.y;
- }
- }
- }
- struct MyData {
- wxPoint p;
- friend class boost::serialization::access;
- template<class Archive>
- void serialize(Archive &ar, unsigned int version){
- ar & p;
- }
- };
- inline void DataSaveTo(const char *filename, MyData data) {
- std::ofstream ofs(filename);
- boost::archive::text_oarchive ar(ofs);
- ar << data;
- }
- inline void DataLoadFrom(const char *filename, MyData data) {
- std::ifstream ifs(filename);
- boost::archive::text_iarchive ar(ifs);
- ar >> data;
- }
- int main(int argc, char **argv)
- {
- if(argc > 1) {
- MyData d;
- DataSaveTo("test.txt", d);
- } else {
- MyData d;
- DataLoadFrom("test.txt", d);
- }
- }
复制代码 其中wxPoint 要用非侵入式的写法, 因为实际这个结构是wxWidgets里的。。
但读取的时候会出现。
terminate called after throwing an instance of 'boost::archive::archive_exception'
what(): class version
Aborted
class version 好像是类版本不同时向后兼容使用的一个参数。。
为什么我这里会出现这个问题呢?
谢谢 |
|