开发者

Compilation error when using boost serialization library

开发者 https://www.devze.com 2022-12-26 12:54 出处:网络
I have been struggling with this error for a long time. The following is my code snippet. //This is the header file

I have been struggling with this error for a long time. The following is my code snippet.

//This is the header file
    template<typename TElem>
class ArrayList {
 public:
  /** An accessible typedef for the elements in the array. */

  typedef TElem Elem;
  friend class boost::serialization::access;
  template<class Archive>
  void serialize(Archive & ar, const unsigned int version)
  {
    ar & ptr_;
    ar & size_;
    ar & cap_;
  }

  Elem *ptr_;    // the stored or aliased array
  index_t size_; // number of active objects
  index_t cap_;  // allocated size of the array; -1 if alias
};

template <typename TElem>
class gps_position
{
public:
    typedef TElem Elem;
    friend class boost::serialization::access;
    template<class Archive>
    void serialize(Archive & ar, const unsigned int version)
    {
      ar & degrees;
      ar & minutes;
      ar & seconds;
   }

private:
    Elem degrees;
    index_t minutes;
    index_t seconds;
};


// This is the .cc file
#include <string>
#include <fstream>
#include <iostream>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/serialization/string.hpp>
#include <boost/serialization/utility.hpp>
#include <boost/serialization/serialization.hpp>
#include "arraylist.h"
int main() {
    // create and open a character archive for output
    std::ofstream ofs("filename");

    // create class instance
//    gps_position<int> g(35.65, 59, 24.567f);
     gps_position<float> g;
    // save data to archive
    {
        boost::archive::text_oarchive oa(ofs);
        // write class instance to archive
        //oa << g;
        // archive and stream closed when destructors are called
    }

    // ... some time later restore the class instance to its orginal state
/*    gps_position<int> newg;
    {
        // create and open an archive for input
        std::ifstream ifs("filename");
        boost::archive::text_iarchive ia(ifs);
        // read class state from archive
        ia >> newg;
        // archive and stream closed when destructors are called
    }*/
    ArrayList<float> a1;
    ArrayList<int> a2;

  a1.Init(22);
  a2.Init(21);

//  a1.Resize(30);
//  a1.Resize(12);
//  a1.Resize(22);
//  a2.Resize(22);
  a1[21] = 99.0;
  a1[20] = 88.0;

  for (index_t i = 0; i < a1.size(); i++) {
    a1[i] = i;
    a1[i]++;
  }

  std::ofstream s("test.txt");
  {
  boost::archive::text_oarchive oa(s);
   oa << a1;

  }
    return 0;
}

The following is the compilation error i get.

In file included from /usr/include/boost/serialization/split_member.hpp:23,
                 from /usr/include/boost/serialization/nvp.hpp:33,
                 from /usr/include/boost/serialization/serialization.hpp:17,
                 from /usr/include/boost/archive/detail/oserializer.hpp:61,
                 from /usr/include/boost/archive/detail/interface_oarchive.hpp:24,
                 from /usr/include/boost/archive/detail/common_oarchive.hpp:20,
                 from /usr/include/boost/archive/basic_text_oarchive.hpp:32,
                 from /usr/include/boost/archive/text_oarchive.hpp:31,
                 from demo.cc:4:
/usr/include/boost/serialization/ac开发者_运维百科cess.hpp: In static member function ‘static void boost::serialization::access::serialize(Archive&, T&, unsigned int) [with Archive = boost::archive::text_oarchive, T = float]’:
/usr/include/boost/serialization/serialization.hpp:74:   instantiated from ‘void boost::serialization::serialize(Archive&, T&, unsigned int) [with Archive = boost::archive::text_oarchive, T = float]’
/usr/include/boost/serialization/serialization.hpp:133:   instantiated from ‘void boost::serialization::serialize_adl(Archive&, T&, unsigned int) [with Archive = boost::archive::text_oarchive, T = float]’
/usr/include/boost/archive/detail/oserializer.hpp:140:   instantiated from ‘void boost::archive::detail::oserializer<Archive, T>::save_object_data(boost::archive::detail::basic_oarchive&, const void*) const [with Archive = boost::archive::text_oarchive, T = float]’
demo.cc:105:   instantiated from here
/usr/include/boost/serialization/access.hpp:109: error: request for member ‘serialize’ in ‘t’, which is of non-class type ‘float’

Please help me out.


You can't serialize a raw pointer to a float, which is what you're trying to do here. In fact you're not actually serializing the array anyway when you try to serialize a1 - you only try to serialize the pointer to the head of it.

I don't know what you're trying to accomplish, but can you just make a1 and a2 raw float arrays? Boost can serialize those natively.

float a1[21];
a1[21] = 99.0;
for (index_t i = 0; i < 21; i++) {
    a1[i] = i;
    a1[i]++;
}

std::ofstream s("test.txt");
boost::archive::text_oarchive oa(s);
oa << a1;

The better option is probably to change your ArrayList definition to use std::vector<Elem> instead of a raw Elem *. Boost::Serialization also already knows how to serialize all STL containers.

For further information see the Boost serialization reference manual.

0

精彩评论

暂无评论...
验证码 换一张
取 消