I am using boost serialization on windows, and I wanted to test my code on linux (ubuntu) and unfortunately it does not compile.
#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;
}
Here is my compile line:
g++ test.cpp -lboost-serialization
And here is what I get:
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()>)’
I am probably missing an include...
You've encountered the Most Vexing Parse. In your main method, change
MyClass obj();
to
MyClass obj;
also, on my Linux system, the link line is
g++ test.cpp -lboost_serialization
Note the underscore instead of dash, like in your example
g++ test.cpp -lboost-serialization
精彩评论