提升序列化编译问题

时间:2010-08-28 17:34:00

标签: c++ xml serialization boost xml-serialization

我在windows上使用boost序列化,我想在linux(ubuntu)上测试我的代码,不幸的是它不能编译。

#include <string>
#include <fstream>

#include <boost/archive/xml_iarchive.hpp>
#include <boost/archive/xml_oarchive.hpp>


#include <boost/serialization/string.hpp>
#include <boost/serialization/nvp.hpp>
#include <boost/serialization/export.hpp>


class MyClass
{
  public:
    int something;
    MyClass();

    template<class archive> void serialize(archive& ar, const unsigned int version)
    {
      using boost::serialization::make_nvp;
      ar & make_nvp("something", something);
    }


};



MyClass::MyClass()
  : something(0)
{
}

int main(int argc, char ** argv)
{
  std::cout << "Hello";
  MyClass obj();

  std::string path = "here.xml";

  std::ifstream ifs(path.c_str());
  if (ifs.good())
  {
    boost::archive::xml_iarchive ia(ifs);
    ia >> boost::serialization::make_nvp("obj", obj);

    ifs.close();
    return true;
  }
  else
    return false;

}

这是我的编译行:

g++ test.cpp -lboost-serialization

这就是我得到的:

In file included from /usr/include/boost/archive/detail/interface_iarchive.hpp:23,
                 from /usr/include/boost/archive/detail/common_iarchive.hpp:21,
                 from /usr/include/boost/archive/basic_xml_iarchive.hpp:23,
                 from /usr/include/boost/archive/xml_iarchive.hpp:24,
                 from test.cpp:6:
/usr/include/boost/archive/detail/iserializer.hpp: In function ‘void boost::archive::load(Archive&, const T&) [with Archive = boost::archive::xml_iarchive, T = MyClass()]’:
/usr/include/boost/archive/detail/common_iarchive.hpp:61:   instantiated from ‘void boost::archive::detail::common_iarchive<Archive>::load_override(T&, int) [with T = MyClass(), Archive = boost::archive::xml_iarchive]’
/usr/include/boost/archive/basic_xml_iarchive.hpp:81:   instantiated from ‘void boost::archive::basic_xml_iarchive<Archive>::load_override(const boost::serialization::nvp<T>&, int) [with T = MyClass(), Archive = boost::archive::xml_iarchive]’
/usr/include/boost/archive/xml_iarchive.hpp:75:   instantiated from ‘void boost::archive::xml_iarchive_impl<Archive>::load_override(T&, int) [with T = const boost::serialization::nvp<MyClass()>, Archive = boost::archive::xml_iarchive]’
/usr/include/boost/archive/detail/interface_iarchive.hpp:61:   instantiated from ‘Archive& boost::archive::detail::interface_iarchive<Archive>::operator>>(T&) [with T = const boost::serialization::nvp<MyClass()>, Archive = boost::archive::xml_iarchive]’
test.cpp:48:   instantiated from here
/usr/include/boost/archive/detail/iserializer.hpp:570: error: no matching function for call to ‘load_wrapper(boost::archive::xml_iarchive&, MyClass (&)(), boost::serialization::is_wrapper<MyClass()>)’

我可能错过了一个包含...

1 个答案:

答案 0 :(得分:4)

您遇到了Most Vexing Parse。在您的主要方法中,更改

MyClass obj();

MyClass obj;

另外,在我的Linux系统上,链接行是

g++ test.cpp -lboost_serialization

请注意下划线而不是短划线,就像您的示例

一样
g++ test.cpp -lboost-serialization