0

我遇到了一个带有模板类的泡菜:

template <
          class N
         >
class Edge
{
  public:

    typedef std::shared_ptr<N> N_ptr;

    Edge
    (
      N_ptr node
     )
    {
       //...
    }

    template <class Archive>
    static Edge<N> * load_and_allocate( Archive & ar )
    {
      N_ptr node;
      ar( node );
      return new ::template Edge<N>( node );
    }
};

谷物需要方法 load_and_allocate来反序列化没有默认空构造函数的对象。

对于 load_and_allocate 的返回行,编译器抛出:

/usr/include/c++/4.7/bits/stl_construct.h: In instantiation of ‘void std::_Construct(_T1*, _Args&& ...) [with _T1 = Edge<Concept>; _Args = {}]’:
/usr/include/c++/4.7/bits/stl_uninitialized.h:497:3:   required from ‘static void std::__uninitialized_default_n_1<_TrivialValueType>::__uninit_default_n(_ForwardIterator, _Size) [with _ForwardIterator = Edge<Concept>*; _Size = long unsigned int; bool _TrivialValueType = false]’
/usr/include/c++/4.7/bits/stl_uninitialized.h:545:7:   required from ‘void std::__uninitialized_default_n(_ForwardIterator, _Size) [with _ForwardIterator = Edge<Concept>*; _Size = long unsigned int]’
/usr/include/c++/4.7/bits/stl_uninitialized.h:607:7:   required from ‘void std::__uninitialized_default_n_a(_ForwardIterator, _Size, std::allocator<_Tp>&) [with _ForwardIterator = Edge<Concept>*; _Size = long unsigned int; _Tp = Edge<Concept>]’
/usr/include/c++/4.7/bits/vector.tcc:541:8:   required from ‘void std::vector<_Tp, _Alloc>::_M_default_append(std::vector<_Tp, _Alloc>::size_type) [with _Tp = Edge<Concept>; _Alloc = std::allocator<Edge<Concept> >; std::vector<_Tp, _Alloc>::size_type = long unsigned int]’
/usr/include/c++/4.7/bits/stl_vector.h:647:4:   required from ‘void std::vector<_Tp, _Alloc>::resize(std::vector<_Tp, _Alloc>::size_type) [with _Tp = Edge<Concept>; _Alloc = std::allocator<Edge<Concept> >; std::vector<_Tp, _Alloc>::size_type = long unsigned int]’
/usr/local/include/cereal/types/vector.hpp:83:5:   [ skipping 31 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/Daemon/../ConnectionHandler/../../Processes/Controller/../../Datatypes/Domain/../../Handlers/SemanticNodeFactory/SemanticNodeFactory.hpp:34:82:   required from here
/usr/include/c++/4.7/bits/stl_construct.h:77:7: error: no matching function for call to ‘Edge<Concept>::Edge()’
/usr/include/c++/4.7/bits/stl_construct.h:77:7: note: candidates are:
candidate expects 1 argument, 0 provided
Edge<Relation>::Edge(const Edge<Relation>&)

其他模板(概念和关系)的消息重复。

同样的事情发生在:

return new ::template Edge<N>( node );

return new Edge<N>::template Edge( node );

或者

return new Edge<N>::template Edge<N>( node );

或者

return new Edge<N>( node );

事实上,大多数人都抱怨限定符,只有第一个 return 语句没有产生任何关于限定符的错误。

有人可以解释为什么它没有选择唯一可用的构造函数,并忽略我传递给它的参数吗?

奇怪的是,如果我声明一个空的默认公共构造函数:

Edge( ){ }

它编译没有错误。

4

1 回答 1

2

调整 T 向量的大小需要 T 是默认可构造的,或者您提供默认值作为“resize”的第二个参数。

在您的情况下,Edge 不是默认可构造的。

于 2013-11-07T21:12:32.363 回答