【问题标题】:Declaration of container with same type as the class in that class only仅声明与该类中的类具有相同类型的容器
【发布时间】:2018-04-25 15:08:08
【问题描述】:

作为文件一部分的以下代码行产生错误

.\Graph.h:16:3: note: candidate: node::node(int)
   node(int n) : element(n){
   ^~~~
.\Graph.h:16:3: note:   candidate expects 1 argument, 0 provided
.\Graph.h:10:8: note: candidate: constexpr node::node(const node&)
 struct node{
        ^~~~
.\Graph.h:10:8: note:   candidate expects 1 argument, 0 provided
.\Graph.h:10:8: note: candidate: constexpr node::node(node&&)
.\Graph.h:10:8: note:   candidate expects 1 argument, 0 provided

上述错误通常是由于没有我研究过的默认构造函数而发生的。

struct node{
  int element;
  static vector<bool> check;
  static vector<int> dist;
  static vector<int> f;
  static vector<node> leader;
  node(int n) : element(n){
    if(check.size()<n+1){
      check.resize(n+1);
      leader.resize(n+1);
    }
  }
  bool operator < (node& n){
    if(this->element<n.element)
      return 1;
    else
      return 0;
  }
};

但是,默认构造函数不会在其他任何地方使用。另外,当我注释掉static vector&lt;node&gt; leader和其他与之相关的操作时,程序没有报错。

那么,问题是因为我在同一个类中声明了一个相同类型的容器吗?

编辑 - 包含完整的错误消息

In file included from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\vector:62:0,
                 from .\Graph.h:5:
c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\stl_construct.h: In instantiation of 'void std::_Construct(_T1*, _Args&& ...) [with _T1 = node; _Args = {}]':
c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\stl_uninitialized.h:519:18:   required from 'static _ForwardIterator std::__uninitialized_default_n_1<_TrivialValueType>::__uninit_default_n(_ForwardIterator, _Size) [with _ForwardIterator = node*; _Size = unsigned int; bool _TrivialValueType = false]'
c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\stl_uninitialized.h:575:20:   required from '_ForwardIterator std::__uninitialized_default_n(_ForwardIterator, _Size) [with _ForwardIterator = node*; _Size = unsigned int]'
c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\stl_uninitialized.h:637:44:   required from '_ForwardIterator std::__uninitialized_default_n_a(_ForwardIterator, _Size, std::allocator<_Tp>&) [with _ForwardIterator = node*; _Size = unsigned int; _Tp = node]'
c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\vector.tcc:549:35:   required from 'void std::vector<_Tp, _Alloc>::_M_default_append(std::vector<_Tp, _Alloc>::size_type) [with _Tp = node; _Alloc = std::allocator<node>; std::vector<_Tp, _Alloc>::size_type = unsigned int]'
c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\stl_vector.h:677:21:   required from 'void std::vector<_Tp, _Alloc>::resize(std::vector<_Tp, _Alloc>::size_type) [with _Tp = node; _Alloc = std::allocator<node>; std::vector<_Tp, _Alloc>::size_type = unsigned int]'
.\Graph.h:19:24:   required from here
c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\stl_construct.h:75:7: error: no matching function for call to 'node::node()'
     { ::new(static_cast<void*>(__p)) _T1(std::forward<_Args>(__args)...); }
       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.\Graph.h:16:3: note: candidate: node::node(int)
   node(int n) : element(n){
   ^~~~
.\Graph.h:16:3: note:   candidate expects 1 argument, 0 provided
.\Graph.h:10:8: note: candidate: constexpr node::node(const node&)
 struct node{
        ^~~~
.\Graph.h:10:8: note:   candidate expects 1 argument, 0 provided
.\Graph.h:10:8: note: candidate: constexpr node::node(node&&)
.\Graph.h:10:8: note:   candidate expects 1 argument, 0 provided
In file included from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\vector:62:0,
                 from .\Graph.h:5:
c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\stl_construct.h: In instantiation of 'void std::_Construct(_T1*, _Args&& ...) [with _T1 = node; _Args = {}]':
c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\stl_uninitialized.h:519:18:   required from 'static _ForwardIterator std::__uninitialized_default_n_1<_TrivialValueType>::__uninit_default_n(_ForwardIterator, _Size) [with _ForwardIterator = node*; _Size = unsigned int; bool _TrivialValueType = false]'
c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\stl_uninitialized.h:575:20:   required from '_ForwardIterator std::__uninitialized_default_n(_ForwardIterator, _Size) [with _ForwardIterator = node*; _Size = unsigned int]'
c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\stl_uninitialized.h:637:44:   required from '_ForwardIterator std::__uninitialized_default_n_a(_ForwardIterator, _Size, std::allocator<_Tp>&) [with _ForwardIterator = node*; _Size = unsigned int; _Tp = node]'
c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\vector.tcc:549:35:   required from 'void std::vector<_Tp, _Alloc>::_M_default_append(std::vector<_Tp, _Alloc>::size_type) [with _Tp = node; _Alloc = std::allocator<node>; std::vector<_Tp, _Alloc>::size_type = unsigned int]'
c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\stl_vector.h:677:21:   required from 'void std::vector<_Tp, _Alloc>::resize(std::vector<_Tp, _Alloc>::size_type) [with _Tp = node; _Alloc = std::allocator<node>; std::vector<_Tp, _Alloc>::size_type = unsigned int]'
.\Graph.h:19:24:   required from here
c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\stl_construct.h:75:7: error: no matching function for call to 'node::node()'
     { ::new(static_cast<void*>(__p)) _T1(std::forward<_Args>(__args)...); }
       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.\Graph.h:16:3: note: candidate: node::node(int)
   node(int n) : element(n){
   ^~~~
.\Graph.h:16:3: note:   candidate expects 1 argument, 0 provided
.\Graph.h:10:8: note: candidate: constexpr node::node(const node&)
 struct node{
        ^~~~
.\Graph.h:10:8: note:   candidate expects 1 argument, 0 provided
.\Graph.h:10:8: note: candidate: constexpr node::node(node&&)
.\Graph.h:10:8: note:   candidate expects 1 argument, 0 provided

【问题讨论】:

  • 下次请附上完整的错误信息。您发布的所有内容都是补充说明,而不是实际错误,它与error: no matching constructor for initialization of 'node' 和一堆std::vector-internal 模板实例化提示类似。是的,这些带有更多代码的错误看起来很可怕,但它们的存在是有原因的。
  • 旁白:你的operator&lt;可以简化为return element &lt; n.element;
  • 如果您希望在创建节点时添加leader,则不能将其设为vector&lt;node&gt;,因为您将添加副本,并且每个副本都将添加到@ 987654332@,并且您的程序继续运行,直到它溢出堆栈或无法重新分配 leader 的元素
  • 可能可以在构造函数中将其设为std::vector&lt;std::reference_wrapper&lt;node&gt;&gt;leader.push_back(*this)。但请注意不要让引用超过nodes
  • @GauravPant 但它们是相关的,请参见此处:godbolt.org/g/rSgo7G - 第一个是问题(无法构造 node),最后三个告诉您可用的构造函数以及它们的原因无法使用。中间长的确实有点吵,但是下一个是最重要的:in instantiation of member function 'std::vector&lt;node, std::allocator&lt;node&gt; &gt;::resize' requested here: leader.resize(n+1);。这直接告诉你resize函数是问题所在。

标签: c++ constructor containers


【解决方案1】:

leader.resize(n+1); 将调整向量的大小,使其具有 n+1 个默认构造的nodes。因此,如果您想拥有这一行,则需要一个默认构造函数。

但是,如果没有更多关于您期望这个向量是什么或如何处理它的信息,很难知道应该如何修复它。

注意:您可以通过将node() = default; 添加到您的节点类来添加默认构造函数。

【讨论】:

  • 是否允许声明任何与该类相同类型的容器?
  • 我认为默认构造函数在这里不是一个好主意,元素成员的正确值是多少?也许两个参数 resize() 更合适
  • @gaurav:显然节点类不能包含 2 个节点的 std::array,因为这将包括这 2 个节点中的 4 个节点等等。
  • @GauravPant 您可以声明该类实例的容器,但不能在每次构造容器时都向该容器添加实例。因为该插入将导致对构造函数的另一次调用(因为它在容器中创建了一个新实例),这将添加一个元素,这将导致对构造函数的另一次调用,等等。
  • @MSalters,这两个参数 resize() 有效(更好的解决方案)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-07-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-17
  • 2020-01-04
相关资源
最近更新 更多