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