2011-04-08 69 views
1

所以......升压系列化stringstream的错误

每当我运行以下命令:

#inlcude <iostream> 
#include <sstream> 

#include <boost/archive/text_oarchive.hpp> 
#include <boost/archive/text_iarchive.hpp> 

using namespace std; 

class gps_position 
{ 
private: 
    friend class boost::serialization::access; 
    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() { 
    stringstream ss1; 

    const gps_position g(35, 59, 24.567f); 

    { 
     boost::archive::text_oarchive oa(ss1); 
     oa << g; 
    } 

    gps_position newg; 
    { 
     stringstream ss2; 
     boost::archive::text_iarchive ia(ss2); 
     ia >> newg; 
    } 
    return 0; 
} 

我得到以下错误:

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

所以......这令我感到困惑..任何帮助都会很棒!

谢谢!

回答

4

main您填充ss1,然后创建一个新的std::stringstream调用ss2并尝试从中读取。你会如何期待这个工作?很明显,ss2不包含任何数据。