boost序列化错误C4308:负积分常量转换为无符号类型

时间:2014-10-28 10:03:54

标签: c++ serialization boost

所以我最近尝试使用boost :: serialization序列化我的类,并且正在阅读本教程:http://en.highscore.de/cpp/boost/serialization.html。 我可以在这个页面中编译代码但是我无法编译自己编写的代码。例如,在“类层次结构对象的序列化”一节中,我们有以下代码:

#include <boost/archive/text_oarchive.hpp> 
#include <boost/archive/text_iarchive.hpp> 
#include <boost/serialization/string.hpp> 
#include <iostream> 
#include <sstream> 
#include <string> 

std::stringstream ss; 

class person 
{ 
public: 
  person() 
  { 
  } 

  person(int age) 
    : age_(age) 
  { 
  } 

  int age() const 
  { 
    return age_; 
  } 

private: 
  friend class boost::serialization::access; 

  template <typename Archive> 
  void serialize(Archive &ar, const unsigned int version) 
  { 
    ar & age_; 
  } 

  int age_; 
}; 

class developer 
  : public person 
{ 
public: 
  developer() 
  { 
  } 

  developer(int age, const std::string &language) 
    : person(age), language_(language) 
  { 
  } 

  std::string language() const 
  { 
    return language_; 
  } 

private: 
  friend class boost::serialization::access; 

  template <typename Archive> 
  void serialize(Archive &ar, const unsigned int version) 
  { 
    ar & boost::serialization::base_object<person>(*this); 
    ar & language_; 
  } 

  std::string language_; 
}; 

void save() 
{ 
  boost::archive::text_oarchive oa(ss); 
  developer d(31, "C++"); 
  oa << d; 
} 

void load() 
{ 
  boost::archive::text_iarchive ia(ss); 
  developer d; 
  ia >> d; 
  std::cout << d.age() << std::endl; 
  std::cout << d.language() << std::endl; 
} 

int main() 
{ 
  save(); 
  load(); 
} 

它工作正常,但我可以说我写了这段代码:

#include <boost/archive/xml_oarchive.hpp> 
#include <boost/archive/xml_iarchive.hpp> 
#include <boost/serialization/export.hpp> 
#include <iostream> 
#include <fstream> 
#include <boost/serialization/string.hpp> 
#include <string> 

class A
{
   friend class boost::serialization::access;
public:
   std::string a;
private:
   template<class Archive>
   void serialize(Archive& archive, const unsigned int version)
   {
        archive & a;
   }
};

class B : public A
{
   friend class boost::serialization::access;
public:
   std::string b;
private:
   template<class Archive>
   void serialize(Archive& archive, const unsigned int version)
   {
        archive & boost::serialization::base_object<A>(*this);
        archive & b;
   }
};

BOOST_CLASS_EXPORT(B) 

void save() 
{ 
  std::ofstream file("archive.xml"); 
  boost::archive::xml_oarchive oa(file); 
  B *myB = new B();
  myB->a = "1";
  myB->a = "2";

  oa << myB; 
  delete myB;
  file.close();
} 

void load() 
{ 
  std::ifstream file("archive.xml"); 
  boost::archive::xml_iarchive ia(file); 
  A *myB;

  ia >> myB;
  std::cout << myB->a << std::endl;
}

int main()
{
   save();
   load();
   std::cin.get();
}

当我编译它时,我收到此错误:

error C4308: negative integral constant converted to unsigned type  c:\program files\boost_1_55_0\boost\mpl\print.hpp

你能告诉我我做错了什么吗?

1 个答案:

答案 0 :(得分:0)

被编译错误所困扰(目前还没有MSVC方便尝试)。

在一天结束时,我能够使用MSVC2013进行测试。完整错误(警告)显示为:

boost_1_55_0\boost\mpl\print.hpp(51): warning C4308: negative integral constant converted to unsigned type
          boost_1_55_0\boost\serialization\static_warning.hpp(92) : see reference to class template instantiation 'boost::mpl::print<boost::serialization::BOOST_SERIALIZATION_STATIC_WARNING_LINE<137>>' being compiled
          boost_1_55_0\boost\serialization\export.hpp(137) : see reference to class template instantiation 'boost::serialization::static_warning_test<false,137>' being compiled
          boost_1_55_0\boost\serialization\export.hpp(136) : while compiling class template member function 'const boost::archive::detail::extra_detail::guid_initializer<B> &boost::archive::detail::extra_detail::guid_initializer<B>::export_guid(void) const'
          so.cpp(40) : see reference to function template instantiation 'const boost::archive::detail::extra_detail::guid_initializer<B> &boost::archive::detail::extra_detail::guid_initializer<B>::export_guid(void) const' being compiled
          so\so.cpp(40) : see reference to class template instantiation 'boost::archive::detail::extra_detail::guid_initializer<B>' being compiled

export.hpp中的行显示:

    BOOST_STATIC_WARNING(boost::is_polymorphic< T >::value);

你去了,你试图通过poitner-to-base来序列化多态类,除非RTTI可用,否则没有意义。为此,该类至少需要1个虚方法。


修复样本以使其正常工作:

我注意到缺少NVP封装器。这是一个编译版本: Live On Coliru

#include <boost/archive/xml_oarchive.hpp>
#include <boost/archive/xml_iarchive.hpp>
#include <boost/serialization/export.hpp>
#include <iostream>
#include <fstream>
#include <boost/serialization/string.hpp>
#include <string>

class A {
    friend class boost::serialization::access;

  public:
    std::string a;
    virtual ~A() {}

  private:
    template <class Archive>
    void serialize(Archive &archive, const unsigned int version)
    {
        archive &BOOST_SERIALIZATION_NVP(a);
    }
};

class B : public A {
    friend class boost::serialization::access;

  public:
    std::string b;

  private:
    template <class Archive>
    void serialize(Archive &archive, const unsigned int version)
    {
        using namespace boost::serialization;
        archive &make_nvp("base", base_object<A>(*this));
        archive &BOOST_SERIALIZATION_NVP(b);
    }
};

BOOST_CLASS_EXPORT(B)

void save()
{
    std::ofstream file("archive.xml", std::ios::binary);
    boost::archive::xml_oarchive oa(file);
    B *myB = new B();
    myB->a = "1";
    myB->b = "2";

    A* myA = myB;
    oa << boost::serialization::make_nvp("myA", myA);
    delete myB;
    file.close();
}

void load()
{
    std::ifstream file("archive.xml", std::ios::binary);
    boost::archive::xml_iarchive ia(file);
    A *myA;

    ia >> boost::serialization::make_nvp("myA", myA);

    std::cout << "a: " << myA->a << std::endl;
    if (B* myB = dynamic_cast<B*>(myA))
        std::cout << "b: " << myB->b << std::endl;
}

int main()
{
    save();
    load();
}

请注意,您需要(以及)序列化与反序列化相同的类型(A*)。

通过使A成为虚拟类,您可以使用运行时强制转换:

    std::cout << "a: " << myA->a << std::endl;
    if (B* myB = dynamic_cast<B*>(myA))
        std::cout << "b: " << myB->b << std::endl;

为了完整起见,请点击相应的archive.xml

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE boost_serialization>
<boost_serialization signature="serialization::archive" version="10">
<myA class_id="1" class_name="B" tracking_level="1" version="0" object_id="_0">
    <base class_id="0" tracking_level="1" version="0" object_id="_1">
        <a>1</a>
    </base>
    <b>2</b>
</myA>