std :: make_sharedがデフォルトのコンストラクターを必要とする理由はありますか?

StackOverflow https://stackoverflow.com/questions/19835114

質問

これがからの要件であるかどうかを把握しようとしています 穀物 か否か。

クラスコンストラクター(デフォルトのコンストラクター)がプライベートであるというエラーが発生し続けています。

ただし、エラーの発信線は、デフォルトのコンストラクターを必要とするシリアルではなく、STD :: make_sharedであるように思われますが、すでに友人のクラスであるため、アクセスできるはずです。

/usr/include/c++/4.7/type_traits: In instantiation of ‘struct std::__is_default_constructible_impl<Concept>’:
/usr/include/c++/4.7/type_traits:116:12:   required from ‘struct std::__and_<std::__not_<std::is_void<Concept> >, std::__is_default_constructible_impl<Concept> >’
/usr/include/c++/4.7/type_traits:682:12:   required from ‘struct std::__is_default_constructible_atom<Concept>’
/usr/include/c++/4.7/type_traits:703:12:   required from ‘struct std::__is_default_constructible_safe<Concept, false>’
/usr/include/c++/4.7/type_traits:709:12:   required from ‘struct std::is_default_constructible<Concept>’
/usr/local/include/cereal/types/polymorphic.hpp:157:5:   required by substitution of ‘template<class Archive, class T> typename std::enable_if<((! std::is_default_constructible<T>::value) && (! has_load_and_allocate<T, Archive>())), bool>::type cereal::polymorphic_detail::serialize_wrapper(Archive&, std::shared_ptr<_Tp2>&, uint32_t) [with Archive = cereal::XMLInputArchive; T = Concept]’
/usr/local/include/cereal/types/polymorphic.hpp:253:5:   [ skipping 16 instantiation contexts ]
/usr/include/c++/4.7/bits/shared_ptr_base.h:525:8:   required from ‘std::__shared_count<_Lp>::__shared_count(std::_Sp_make_shared_tag, _Tp*, const _Alloc&, _Args&& ...) [with _Tp = SemanticGraph<Concept>; _Alloc = std::allocator<SemanticGraph<Concept> >; _Args = {const char (&)[16]}; __gnu_cxx::_Lock_policy _Lp = (__gnu_cxx::_Lock_policy)2u]’
/usr/include/c++/4.7/bits/shared_ptr_base.h:997:35:   required from ‘std::__shared_ptr<_Tp, _Lp>::__shared_ptr(std::_Sp_make_shared_tag, const _Alloc&, _Args&& ...) [with _Alloc = std::allocator<SemanticGraph<Concept> >; _Args = {const char (&)[16]}; _Tp = SemanticGraph<Concept>; __gnu_cxx::_Lock_policy _Lp = (__gnu_cxx::_Lock_policy)2u]’
/usr/include/c++/4.7/bits/shared_ptr.h:317:64:   required from ‘std::shared_ptr<_Tp>::shared_ptr(std::_Sp_make_shared_tag, const _Alloc&, _Args&& ...) [with _Alloc = std::allocator<SemanticGraph<Concept> >; _Args = {const char (&)[16]}; _Tp = SemanticGraph<Concept>]’
/usr/include/c++/4.7/bits/shared_ptr.h:599:39:   required from ‘std::shared_ptr<_Tp1> std::allocate_shared(const _Alloc&, _Args&& ...) [with _Tp = SemanticGraph<Concept>; _Alloc = std::allocator<SemanticGraph<Concept> >; _Args = {const char (&)[16]}]’
/usr/include/c++/4.7/bits/shared_ptr.h:615:42:   required from ‘std::shared_ptr<_Tp1> std::make_shared(_Args&& ...) [with _Tp = SemanticGraph<Concept>; _Args = {const char (&)[16]}]’
/home/alex/projects/Icarus/trunk/Api/ConnectionHandler/../../Processes/Controller/../../Datatypes/Domain/../../Handlers/SemanticNodeFactory/SemanticNodeFactory.hpp:34:82:   required from here
/home/alex/projects/Icarus/trunk/Api/ConnectionHandler/../../Processes/Controller/../../Datatypes/Domain/../Episode/../State/../ConceptGraph/../Concept/Concept.hpp:27:5: error: ‘Concept::Concept()’ is private

誰かが私にこれが起こっている理由を説明してもらえますか?さらに重要なことに、それらのコンストラクターを公開することを除けば、どうすれば解決できますか?

編集:

発信エラー行は次のものです。

concept_map  = std::make_shared<SemanticGraph<Concept>>( "concept_map.xml" );

セマンティックグラフctorは次のとおりです。

    SemanticGraph
    (
      const std::string filename
    )
    : 
      _fname ( filename )
    {
      std::ifstream input( _fname );
      if ( input.is_open() )
      {
       cereal::XMLInputArchive archive( input );
       archive( _nodes );
      }
    }
役に立ちましたか?

解決

C ++は、必要に応じてエラーを生成する場合を除き、インスタンス化テンプレートの場合はアクセス制御を考慮しないために使用されます。使用しているコンパイラは、それらのルールをまだ使用しています。そのため、あなたのクラスは定義と構築できないとは見なされません。代わりに、チェック自体は不可能です。

GCC 4.8以降はこれをサポートしています。 4.8で成功し、4.7で失敗する簡単なデモプログラムは次のとおりです。

#include <type_traits>

class S { S() {} };

int main() {
  return std::is_default_constructible<S>::value;
}

4.8では、これは0を返します。4.7では、コンパイル時間エラーが生成されます。

これを解決するには、デフォルトのコンストラクターを持っていないことを確認してください。ダミーの引数をコンストラクターに追加し、常にそのダミー引数を常に渡すことができます。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top