gpt4 book ai didi

c++ - Boost序列化编译问题

转载 作者:搜寻专家 更新时间:2023-10-31 01:21:43 24 4
gpt4 key购买 nike

我在 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()>)’

我可能遗漏了一个包含...

最佳答案

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

MyClass obj();

MyClass obj;

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

g++ test.cpp -lboost_serialization

注意下划线而不是破折号,就像在您的示例中一样

g++ test.cpp -lboost-serialization

关于c++ - Boost序列化编译问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3591735/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com