c++ - Boost序列化编译报错,一头雾水

标签 c++ serialization boost

好的,基本上:

我有这个简单的例子:

主要.cpp

using namespace VHGO::Resource;

std::list<BaseTable*> tableList;

BigTable* bt1 = new BigTable();
HRESULT hr = S_OK;
hr = bt1->Add(L"TEXTURE", L"..\\Data\\ground.png");
tableList.push_back(bt1);

std::wofstream ofs3(L"VHGOSatData.bif");
boost::archive::xml_woarchive outArch3(ofs3);
outArch3 & BOOST_SERIALIZATION_NVP(tableList);

还有我的序列化类

namespace VHGO
{
 typedef std::wstring String;
 typedef std::map<VHGO::String, VHGO::String> PropertyMap;
    namespace Resource
    {
        class BaseTable
        {
            friend class boost::serialization::access;
            friend std::wostream& operator<<(std::wostream& os, const BaseTable& b );
        private:
            template<class Archive>
            void save(Archive& ar, const unsigned int version) const {}

            template<class Archive>
            void load(Archive& ar, const unsigned int version) {}
        public:
            BaseTable()
            {
            }
            //for boost
            virtual ~BaseTable()
            {
            }

            virtual HRESULT Add(__in const VHGO::String&, __in const VHGO::String&) = 0;
            virtual HRESULT Remove(__in const VHGO::String&) = 0;
            virtual HRESULT Get(__in const VHGO::String&, __out VHGO::String&) = 0;
        };

        std::wostream& operator<<(std::wostream& os, const BaseTable& b )
        {
            UNREFERENCED_PARAMETER(b);
            return os;
        }
        //////////////////////////////////////////////////////////////////////////////////////////

        class BigTable : public BaseTable
        {
            friend class boost::serialization::access;

        private:
            VHGO::PropertyMap m_Values;
            template<class Archive>
            void serialize(Archive& ar, const unsigned int version)
            {
                boost::serialization::split_member(ar, *this, version);
            }


            template<class Archive>
            void save(Archive& ar, const unsigned int version) const
            {
                UNREFERENCED_PARAMETER(version);
                ar << boost::serialization::base_object<const VHGO::Resource::BaseTable>(*this);
                ar << boost::serialization::make_nvp("bigtable", m_Values);

            }

            template<class Archive>
            void load(Archive& ar, const unsigned int version)
            {
                UNREFERENCED_PARAMETER(version);
                ar >> boost::serialization::base_object<BaseTable>(*this);
                ar >> boost::serialization::make_nvp("bigtable", m_Values);
            }

           // BOOST_SERIALIZATION_SPLIT_MEMBER()

        public:
            BigTable(__in const VHGO::PropertyMap& propMap)
                : m_Values(propMap)
            {

            }

            BigTable()
            {

            }

            virtual ~BigTable()
            {

            }
            HRESULT Add(__in const VHGO::String& propKey, __in const VHGO::String& propValue)
            {
               //itadds

      return S_OK;
            }

            HRESULT Remove(__in const VHGO::String& propKey)
            {
                /*insertchecking*/
                return S_OK;
            }

            HRESULT Get(__in const VHGO::String& key, __out VHGO::String& aValue)
            {
                aValue = m_Values[key];
                return S_OK;
            }

            VHGO::PropertyMap GetPropertyMap()
            {
                return m_Values;
            }
        };

这是什么原因,我看了文档,我可以让boost的例子正常工作。但我无法完成这项工作。我四处搜索了好几次,结果好坏参半。但我几乎一无所知。

编译错误是这样的:

Error 1 error C2664: 'boost::mpl::assertion_failed' : cannot convert parameter 1 from 'boost::mpl::failed ************boost::serialization::is_wrapper<T>::* ***********' to 'boost::mpl::assert<false>::type'

我使用 VC9.0,并使用 boost 1.41。

有没有人有什么想法?

编辑

按照建议添加包装器

命名空间 boost { 命名空间序列化 { 模板<> 结构 is_wrapper : mpl::真_ {}; }//命名空间序列化 }//命名空间 boost

还是会导致下面的错误

1>d:\wrkspace\Sources\External\boost\boost/archive/basic_xml_oarchive.hpp(87) : error C2664: 'boost::mpl::assertion_failed' : cannot convert parameter 1 from 'boost::mpl::failed ************boost::serialization::is_wrapper<T>::* ***********' to 'boost::mpl::assert<false>::type'
1>        with
1>        [
1>            T=const std::list<VHGO::Resource::BaseTable *>
1>        ]
1>        No constructor could take the source type, or constructor overload resolution was ambiguous
1>        d:\wrkspace\Sources\External\boost\boost/archive/detail/interface_oarchive.hpp(64) : see reference to function template instantiation 'void boost::archive::basic_xml_oarchive<Archive>::save_override<T>(T &,int)' being compiled
1>        with
1>        [
1>            Archive=boost::archive::xml_woarchive,
1>            T=std::list<VHGO::Resource::BaseTable *>
1>        ]

编辑 2

我屈服了并在 gcc 上尝试了这个,它工作正常。可悲的是,我绝对需要它在工作标准的 VS2008 上工作。

最佳答案

你可以试试,来自 docs :

When serializing an object through a pointer to its base class, the library needs to determine whether or not the base is abstract (i.e. has at least one virtual function). The library uses the type trait macro BOOST_IS_ABSTRACT(T) to do this. Not all compilers support this type trait and corresponding macro. To address this, the macro BOOST_SERIALIZATION_ASSUME_ABSTRACT(T) has been implemented to permit one to explicitly indicate that a specified type is in fact abstract. This will guarentee that BOOST_IS_ABSTRACT will return the correct value for all compilers.

但是,您的问题似乎与:

namespace boost { 
namespace serialization {
template<class T>
struct is_wrapper
 : public mpl::false_
{};
} // namespace serialization
} // namespace boost

For any class T, The default definition of boost::serialization::is_wrapper::value is thus false.

我会尝试显式特化 boost::serialization::is_wrapper<BaseTable> .毕竟,您是通过指向基的指针进行序列化的。

最后,您的基础 (BaseTable) 可能需要一个序列化方法(但也许不需要;boost::serialization 做了一些漂亮的 typeid 魔术);我不确定为什么你有一个空的保存和加载。

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

相关文章:

Java网络对象序列化

c# - ProtoBuf-Net 和 Compact Framework 出现 "Invalid field in source data: 0"错误

c++ - 使用 BOOST 测试异常

boost - Boost Asio SSL 是否免费 OpenSSL 分配?

c++ - 使用 boost C++ 单元测试套件测试非 fatal error 消息

c++ - 通过引用调用类对象的二维数组

c++ - glReadPixels 移动坐标缩放

c++ - 错误内存位置的 Lua 参数

c++ - 当参数是引用时,可变参数模板构造函数选择失败

python - 类似字典的高效存储 scipy/numpy 数组