根據此相關問題(Boost Polygon Serialization)。我正在嘗試使用Boost序列化多邊形。我現在的問題是,我試圖編譯使用自定義的X,Y,點的多邊形的例子,但是編譯器在編譯的時候拋出這個錯誤:Boost多邊形序列化:環
error: 'class boost::geometry::model::ring<boost::geometry::model::d2::point_xy<double> >' has no member named 'serialize'
好像沒有定義任何函數序列化戒指。由於一個環從std :: vector擴展而來,並且如相關問題所述,所以沒有必要爲它的序列化定義一個方法。但編譯器抱怨。
這裏我有關定義多邊形一個完整的例子,它的序列化:
#include <fstream>
#include <boost/serialization/vector.hpp>
#include <boost/serialization/nvp.hpp>
#include <boost/serialization/version.hpp>
#include <boost/serialization/tracking.hpp>
#include <boost/geometry/geometry.hpp>
#include <boost/geometry/geometries/polygon.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/io/wkt/wkt.hpp>
#include <boost/geometry/multi/geometries/multi_polygon.hpp>
#include <boost/geometry/geometries/ring.hpp>
#include <boost/archive/xml_iarchive.hpp>
#include <boost/archive/xml_oarchive.hpp>
typedef boost::geometry::model::d2::point_xy<double> point;
typedef boost::geometry::model::ring<point> ring;
typedef boost::geometry::model::polygon<point> polygon;
namespace boost{
namespace serialization{
template<class Archive>
inline void serialize(Archive & ar, point &point, const unsigned int file_version)
{
std::cout << "Point: Serializing point" << std::endl;
ar & boost::serialization::make_nvp("x", point.x());
ar & boost::serialization::make_nvp("y", point.y());
}
template<class Archive>
inline void serialize(Archive & ar, polygon &t, const unsigned int file_version)
{
std::cout << "Polygon: Serializing outer ring" << std::endl;
ar & boost::serialization::make_nvp("outer", t.outer());
std::cout << "Polygon: Serializing inner rings" << std::endl;
ar & boost::serialization::make_nvp("inners", t.inners());
}
}
}
using namespace boost::geometry;
using namespace boost::archive;
using namespace std;
int main()
{
polygon poly;
append(poly, make<point>(0.0, 0.0));
append(poly, make<point>(5.0, 5.0));
append(poly, make<point>(5.0, 0.0));
correct(poly);
ofstream ofs("polygon.xml");
xml_oarchive oa(ofs);
oa << BOOST_SERIALIZATION_NVP(poly);
}
如何得到這個工作任何想法?
編輯:關於多邊形系列化全功能代碼
#include <fstream>
#include <vector>
#include <boost/serialization/vector.hpp>
#include <boost/serialization/nvp.hpp>
#include <boost/serialization/version.hpp>
#include <boost/serialization/tracking.hpp>
#include <boost/foreach.hpp>
#include <boost/geometry/geometry.hpp>
#include <boost/geometry/geometries/polygon.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/geometries/ring.hpp>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
typedef boost::geometry::model::d2::point_xy<double> point;
typedef boost::geometry::model::ring<point> ring;
typedef boost::geometry::model::polygon<point> polygon;
namespace boost{
namespace serialization{
template<class Archive>
inline void serialize(Archive & ar, point &point, const unsigned int file_version)
{
std::cout << "Point: Serializing point" << std::endl;
ar & const_cast<double &>(point.x());
ar & const_cast<double &>(point.y());
}
template<class Archive>
inline void serialize(Archive & ar, ring &ring, const unsigned int file_version)
{
std::cout << "Ring: Serializing ring" << std::endl;
ar & static_cast<std::vector<point>& >(ring);
}
template<class Archive>
inline void serialize(Archive & ar, polygon &t, const unsigned int file_version)
{
std::cout << "Polygon: Serializing outer ring" << std::endl;
ar & t.outer();
std::cout << "Polygon: Serializing inner rings" << std::endl;
ar & t.inners();
}
}
}
using namespace boost::geometry;
using namespace boost::archive;
using namespace std;
int main()
{
polygon poly;
append(poly, make<point>(0.0, 0.0));
append(poly, make<point>(5.0, 5.0));
append(poly, make<point>(5.0, 0.0));
correct(poly);
BOOST_FOREACH(point& p, poly.outer())
{
std::cout << "point " << p.x() << "," << p.y() << std::endl;
}
ofstream ofs("polygon.dat");
binary_oarchive oa(ofs);
oa << poly;
ofs.close();
polygon polyFromFile;
ifstream ifs("polygon.dat");
binary_iarchive ia(ifs);
ia >> polyFromFile;
BOOST_FOREACH(point& p, polyFromFile.outer())
{
std::cout << "point " << p.x() << "," << p.y() << std::endl;
}
ifs.close();
}
很有洞察力。我喜歡你最後的想法。完全可能值得向Boost Geometry提供建議的那種東西 – sehe
完美的答案!謝謝!我和你的解決方案非常接近,但是我使用「ar&static_cast ...」序列化了這個環,而不是調用序列化方法......它編譯的序列化正常,但是沒有使用反常序錯誤的反序列化代碼進行編譯 –
The與我的方法相同的問題,當調用序列化與靜態轉換時,序列化的代碼似乎工作,但添加反序列化時,它會拋出另一個錯誤,如'basic_binary_iprimiteve.hpp:88:9:錯誤:無效轉換'const void * 'to'void *' –