仅与该类中的类具有相同类型的容器声明

时间:2018-04-25 15:08:08

标签: c++ constructor containers

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

.\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<node> 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

1 个答案:

答案 0 :(得分:4)

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

然而,如果没有更多关于您对此向量的预期或使用该向量的信息,很难知道如何修复它。

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