ฉันมีคลาสแกมมาดังที่แสดงด้านล่าง
ฟังก์ชันโหลดจะส่งข้อผิดพลาดในการแบ่งส่วน เมื่อฟังก์ชัน make_nvp พยายามดีซีเรียลไลซ์ไฟล์ xml ที่มีอยู่ เกิดข้อผิดพลาดเมื่อฉันใช้ std::shared_ptr<std::tuple<double,double,double>> val;
ถ้าแทน val เป็นเพียง std::tuple<double,double,double> val;
ดูเหมือนว่าทุกอย่างจะทำงานได้ดี (แน่นอน ฉันเปลี่ยนฟังก์ชัน getter และ setter ตามนั้น)
ตอนนี้ ฉันได้ถามคำถามมากมายเกี่ยวกับ stackoverflow และฉันได้ค้นหาใน Google และดูตัวอย่างในเอกสารบูสต์ และฉันไม่เข้าใจว่าทำไมฟังก์ชันโหลดจึงทำให้โปรแกรมยุติการทำงานด้วยข้อผิดพลาดในการแบ่งเซ็กเมนต์
หมายเหตุ: โพสต์เก่าๆ บางโพสต์บนอินเทอร์เน็ต (และคำถามเก่าๆ เกี่ยวกับ stackoverflow) ดูเหมือนจะบอกเป็นนัยว่า std::shared_ptr เคยใช้งานไม่ได้กับการเพิ่มอนุกรม ณ เวลานั้น ฉันไม่คิดว่าจะเป็นกรณีนี้ในปี 2560 อย่างไรก็ตาม เพื่อให้แน่ใจ ฉันได้ลองแทนที่ std::shared_ptr ด้วย boost::shared_ptr และข้อผิดพลาดในการแบ่งส่วนยังคงอยู่ที่นั่น
ฉันไม่เข้าใจ/ดูสาเหตุที่เกิดข้อผิดพลาด?
gamma.h
#pragma once
#include <map>
#include <boost/serialization/access.hpp>
#include <boost/serialization/nvp.hpp>
#include <boost/serialization/map.hpp>
#include <boost/serialization/vector.hpp>
#include <boost/serialization/utility.hpp>
#include <boost/serialization/shared_ptr.hpp>
#include <tuple>
namespace boost
{
namespace serialization
{
template<typename Archive>
void serialize(Archive & ar, std::tuple<double, double, double> & t,
const unsigned int version)
{
ar & boost::serialization::make_nvp("t0",std::get<0>(t));
ar & boost::serialization::make_nvp("t1",std::get<1>(t));
ar & boost::serialization::make_nvp("t2",std::get<2>(t));
}
}
}
class Gamma
{
public:
static void save(std::ostream& os);
static void load(std::istream& is);
std::shared_ptr<std::tuple<double, double, double>> getterX() const;
void setterX(const std::tuple<double, double, double> &val);
private:
std::shared_ptr<std::tuple<double,double,double>> val;
friend class boost::serialization::access;
template<typename Archive>
void serialize(Archive& arc, const unsigned int version)
{
arc & boost::serialization::make_nvp("val", val);
}
};
และ gamma.cpp
#include "gamma.h"
#include <boost/archive/xml_iarchive.hpp>
#include <boost/archive/xml_oarchive.hpp>
#include <boost/serialization/access.hpp>
#include <boost/serialization/nvp.hpp>
#include <boost/serialization/utility.hpp>
Gamma &Gamma::instance()
{
static Gamma obj;
return obj;
}
std::shared_ptr<std::tuple<double, double, double>> Gamma::getterX() const
{
return val;
}
void Gamma::setterX(const std::tuple<double, double,double> &v)
{
if (nullptr == val) {
m_touchDownCalibration = std::make_shared<std::tuple<double, double,double>>();
}
*val = v;
}
const char* TAG = "tag";
void Gamma::save(std::ostream& os)
{
boost::archive::xml_oarchive arc(os);
arc & boost::serialization::make_nvp(TAG,instance());
}
void Gamma::load(std::istream& is)
{
boost::archive::xml_iarchive arc(is);
arc & boost::serialization::make_nvp(TAG,instance());
}
main
แบบง่าย ๆ ฉันจะตรวจไม่พบปัญหา - person overseas   schedule 06.03.2017