boost::serializationについて

このQ&Aのポイント
  • テンプレート辺りでコンパイルできないコードを含む
  • STLコンテナのネストはserializeできないのか
  • ヘッダが足りない可能性がある
回答を見る
  • ベストアンサー

boost::serializationについて

次のコードがテンプレート辺りでコンパイルできません #include <list> #include <array> #include <fstream> #include <boost/serialization/serialization.hpp> #include <boost/serialization/array.hpp> #include <boost/serialization/list.hpp> #include <boost/archive/binary_iarchive.hpp> #include <boost/archive/binary_oarchive.hpp> std::list<std::array<bool, 3>> lab; std::array<bool, 3> pusharray; pusharray[0] = true; pusharray[1] = false; pusharray[2] = true; lab.push_back(pusharray); lab.push_back(pusharray); std::ofstream ofs("test"); boost::archive::binary_oarchive boa(ofs); boa << lab; ofs.close(); std::ifstream ifs("test"); boost::archive::binary_iarchive bia(ifs); bia >> lab; ifs.close(); エラーメッセージ: c:\program files\microsoft visual studio 10.0\vc\include\boost\archive\basic_binary_iprimitive.hpp(181): warning C4244: '引数' : 'std::streamsize' から 'size_t' への変換です。データが失われる可能性があります。 c:\program files\microsoft visual studio 10.0\vc\include\boost\archive\basic_binary_iprimitive.hpp(152): クラス テンプレート のメンバー関数 'void boost::archive::basic_binary_iprimitive<Archive,Elem,Tr>::load_binary(void *,size_t)' のコンパイル中 with [ Archive=boost::archive::naked_binary_iarchive, Elem=char, Tr=std::char_traits<char> ] c:\program files\microsoft visual studio 10.0\vc\include\boost\archive\detail\iserializer.hpp(362) : コンパイルされたクラスの テンプレート のインスタンス化 'void boost::archive::load_access::load_primitive<Archive,T>(Archive &,T &)' の参照を確認してください with [ Archive=boost::archive::naked_binary_iarchive, T=boost::archive::object_id_type ] (略) そもそもSTLコンテナのネストはserializeできないのでしょうか それともヘッダが足りないのでしょうか

質問者が選んだベストアンサー

  • ベストアンサー
  • qwertfk
  • ベストアンサー率67% (55/81)
回答No.1

#include <boost/serialization/array.hpp> はstd::arrayではなく、boost::array用のようです。 単にstd::arrayをboost::arrayに変更するか、もしくは自作するしかないかもしれません。 とはいっても、 namespace boost { namespace serialization { template <class Archive, typename T, size_t N> void serialize (Archive& ar, std::array<T, N>& a, const unsigned int version) {  if(Archive::is_loading::value)  {   size_t count;   ar & count;   assert(count == N);  }  else  {   size_t count = N;   ar & count;  }  for(int i = 0; i < N; ++i)  {   ar & a[i];  } } } } こんな感じで特に難しい実装ではないですが。

aryuanother
質問者

お礼

ご回答ありがとうございます 今度はBoostのコンテナに統一したほうがよさそうですね

関連するQ&A

  • boost::archive::xml_oarchiveについて

    boostのコンパイルが通りません。調べられるだけ調べたのですが、原因として表示されるmake_nvpを利用しても、boost::STATIC_ASSERTION_FAILUREが発生します。わかる方ご教授お願いします。 #define BOOST_LIB_NAME boost_serialization #include <boost/config/auto_link.hpp> #include <boost/serialization/string.hpp> #include <boost/serialization/access.hpp> #include <boost/serialization/nvp.hpp> #include <boost/archive/xml_oarchive.hpp> #include <fstream> class FooData { private: int Data; friend class boost::serialization::access; template <class Archive> void serialization(Archive& ar, const unsigned int version) { ar & boost::serialization::make_nvp("Data",Data) } }; void main(void) { FooData data; //アーカイブ作成 std::ofstream ofs("output.txt"); boost::archive::xml_oarchive oa(ofs); // ファイル書き出し oa << (const FooData&)data; ofs.close(); }

  • Cygwin boost

    Windows XP sp2 + Cygwin の C++ で正規表現を使いたく setup.exe で boost をインストールし、以下のコードをコンパイル($g++ regextest.cpp) したのですがエラーが出てしまいました。 コード: #include <boost/regex.hpp> #include <iostream> using namespace std; int main () { string str("Free Programming For Windows."); boost::regex reg("Free.+Windows\\."); if (boost::regex_match(str, reg)) { cout << "===> Matched." << endl; } else { cout << "===> Not matched." << endl; } return 0; } エラー: regextest.cpp:1:27: boost/regex.hpp: No such file or directory パスが通っていないようなのですが、どうしたらよいか分かりません。アドバイスいただけたら嬉しいです。 usr/include/boost-1_33_1 に boost/regex.hpp は確認できました。

  • boost::regexで、日本語に利用ついて

    boost_1_47を利用して、置換を行おうとしているのですが、「VIII」を置換しようとするとエラーになります。たぶん「5B」が入っているからかと思うのですが、どのように対処したらよいのでしょうか? #include <boost/regex.hpp> using namespace std; using namespace boost; const boost::regex str("VIII"); よろしくお願いします。

  • boostを利用したプログラムのコンパイルでエラー

    boostを初めて利用しようと思い、ファイルをダウンロードして、bjamを使ってインストールしました。そして以下のようなテスト用のプログラムを作成しました。 #include <boost/tokenizer.hpp> using namespace boost; int main(void) {   char_separator<char> sep(" \t\n");   tokenizer<char_separator<char> > tokens("aaa bbb ccc", sep);   return 0; } これをbcc32によりコンパイルすると、以下のようなエラーがいくつか発生してコンパイルができませんでした。 エラー E2489 c:\borland\bcc55\include\boost-1_33_1\boost/mpl/aux_/integral_wrapper.hpp 45: オプションコンテキスト応答深度の上限を超過: 再帰をチェックしてください なぜ、このようなエラーが出るのでしょうか。ちなみに、cygwin+gccではコンパイルおよび実行が可能でした。 ご存じの方がいらっしゃったらよろしくお願いします。(WinXPです)

  • boost::formatの値をstring型にコピーしたい

    boost::formatの値をstring型にコピーしたいのですが、うまくいきません。boost自体初めてで以下のサイトからダウンロードし、展開後VS2005のVCのインクルードフォルダーにboostフォルダーをまるまるコピーしただけですが・・・ http://sourceforge.net/project/showfiles.php?group_id=7586 boost 1.34.1 #include <iostream> #include <boost/format.hpp> using namespace std; using boost::format; void main(){ double x = 1.234; string str("abc"); //cout << format("%10.3f, [%16s]") % x % str << endl; // サンプルはこうでした。 // いったんstring型に入れて表示させたい。 string y; y = boost::format("%10.3f, [%16s]") % x % str; // エラー箇所 std::cout << y << std::endl; }

  • ofstream::getがおかしい!

    ofstream::getの動作がおかしいのです #include <string> #include <iostream> #include <fstream> using namespace std; void main(void) { ifstream ifs; ofstream ofs; string str; char c; str="abc"; cout<<"before str: "<<str<<endl<<endl; ofs.open("gomi");ofs<<str;ofs.close(); ifs.open("gomi");for(str="";ifs.eof()==0;str+=c)ifs.get(c);ifs.close(); cout<<"after str: "<<str<<endl<<endl; } の結果が before str: abc after str: abcc になります 最後のcは何でつくのでしょうか? 回避する方法を教えてください

  • VC++ 2015 boostの使用について

    当方の環境は、 windows 7 visual studio community 2015 Eドライブ直下にboostフォルダあり です。 こちらのサイトのサンプルを実行したいのですが、boostの導入に手間取っています。 http://sfpgmr.hatenablog.jp/entry/wasapi-%E6%8E%92%E4%BB%96%E3%83%A2%E3%83%BC%E3%83%89%E3%82%B5%E3%83%B3%E3%83%97%E3%83%AB%EF%BC%88%E3%82%BF%E3%82%A4%E3%83%9E%E3%83%A2%E3%83%BC%E3%83%89%EF%BC%89 ここ(http://rexpit.blog29.fc2.com/blog-entry-89.html)を参考にboostのビルドまでは完了したと思います。 プロジェクトのプロパティページから 構成プロパティ→VC++ディレクトリで インクルードディレクトリに E:\boost ライブラリディレクトリに E:\boost\stage\lib を追加して実行しても「ソースファイルを開けません"cstdint.hpp"」などのエラーが出ます。 サンプルの#include <boost/cstdint.hpp>を#include <cstdint.hpp>に変更するなど試してみましたが同様でした。 どなたか正しい設定をご教授いただけると幸いです。

  • boost::pointee

    boost/pointee.hpp を読んでいて、分からないところがあります。 ... template<class Iterator> struct iterator_pointee { ... struct impl { template<class T> static char test(T const&); static char (& test(value_type&) )[2]; // ここ static Iterator& x; }; BOOST_STATIC_CONSTANT(bool, is_constant=sizeof(impl::test(*impl::x)) == 1); .... }; ... この impl というクラスがどういう経緯でこういう実装になっているのかが分かりません。「ここ」と書いたところはどうして必要なのでしょうか。。。 このコードは const をつけるかどうかを判断するための条件のようですが、これで const が必要かどうかが分かるようになる仕組みまで教えていただけると助かります。 よろしくお願いします。

  • boostスマートポインタ(shared_ptr)を使用した、簡単なフ

    boostスマートポインタ(shared_ptr)を使用した、簡単なファイルの読み込みとループ処理で困っています。 以下に示すような、テキストファイルから情報を読み取って構造体のvectorをつくる処理をshared_ptrを使って行おうとしています。 input.txt-- >Taro /boy /8 years old >Hanako /girl /6 years old /likes candy ----- このファイルの'>'で始まる行は'名前'として、それ以外の '/ 'で始まる行はvectorに入れて、このふたつを個人ごとにまとめて構造体として管理するという形をとりたいと考えています。 つまり構造体の要素は <名前の行> <それ以外の行が入ったベクター> となります。 ファイル読み込みの過程で '>' を認識したらこれらのメモリ領域を確保し、shared_ptrで管理したいと考えています。 出来た構造体をベクターに入れ、ファイル読み込みのあとに出力する、という形でプログラムを以下のように書きました。  #include <string> #include <fstream> #include <iostream> #include <boost/shared_ptr.hpp> using namespace std; //構造体定義  struct my_struct{ std::string name;    std::vector<std::string> vector_of_string;  } ; int main(void){ std::ifstream ifs("input.txt"); //構造体を入れるベクター std::vector<my_struct> vector_of_my_struct; std::string buf; boost::shared_ptr<my_struct> SMARTptr_my_struct(new my_struct); boost::shared_ptr<vector<string>> SMARTptr_vector_of_string(new vector<string>); while(ifs && std::getline(ifs, buf)) { if(buf[0] == '>'){   //構造体の領域を作成 boost::shared_ptr<my_struct> SMARTptr_my_struct(new my_struct); //'/'で始まる行を格納しておくベクター領域の作成 boost::shared_ptr<vector<string>> SMARTptr_vector_of_string(new vector<string>); //構造体に作成されたベクターを入れる (*SMARTptr_my_struct).vector_of_string = *SMARTptr_vector_of_string; //構造体にこの行ををnameとして入れる (*SMARTptr_my_struct).name = buf; //ここで新たに作られた構造体をベクターに入れる vector_of_my_struct.push_back(*SMARTptr_my_struct); } if(buf[0] == '/'){ //この行をstring格納用のベクター(if(buf[0]=='>') ですでに作られている)に入れる (*SMARTptr_vector_of_string).push_back(buf); } この後、構造体のベクターのループから情報を出力しますが字数制限のため省略させていただきます。 input.txtと同じ内容が出力されることを期待したのですが、実際は'>'で始まる名前の行しか出力されません。 どうやらベクターへのpush_backができていないか、そもそもshared_ptrの扱いに根本的な間違いがあるのでは?と思っているのですが、原因がわかりません。環境はVC++2008 express edition, boost 1.38です。 解決方法、原因、アドバイスなど分かりましたらよろしくお願いします。

  • std::wstringの継承

    #include<iostream> #include<string> #include <stdlib.h> #include <locale.h> #include <boost/lexical_cast.hpp> using boost::lexical_cast; using namespace std; VisualC++2008ExpressEditionで文字や数字を簡単に扱えるクラスを今作ろうとしていて以下のように作ってみました。 class multiString:public std::wstring{ public:   multiString(const wchar_t *ws){     /* multiString class自体に代入 */   } }; しかし、このwchar_tをクラスに代入する処理として、 multiString(const wchar_t *s)std::wstring(s); とすると、「error C2082: 仮パラメータ 's' が再定義されました。」となりますし、 multiString(const wchar_t *s)*this=s; とすると、例外が発生してしまします。 wstringだったら、簡単に代入処理として出来るのですが、継承した場合はどの様に実装すればいいのでしょうか? 宜しくお願いします。

専門家に質問してみよう