سؤال

أحاول معرفة ما إذا كان هذا مطلبًا من حبوب أم لا.

ما زلت أحصل على أخطاء أن منشئو الفصل (الافتراضيون) خاصون ، والتي وضعتها هناك لسبب ما.

ومع ذلك ، يبدو أن الخط الأصلي للخطأ ، 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