解析编译错误:没有匹配函数可调用'std :: pair<,> :: pair()' [英] Parsing compilation error: no matching function for call to 'std::pair<,>::pair()'

查看:125
本文介绍了解析编译错误:没有匹配函数可调用'std :: pair<,> :: pair()'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是> assigning-of-unordered-map的后续问题对对象.这是关于编译器错误解释的问题(而不是重复问题,因为该问题已得到完全回答).有人问我是否看过错误,并发布错误,以便其他人可以从了解中受益.这是第一个错误:

This is a followup question to assigning-of-unordered-map-to-pair-of-objects. This is a question about the interpretation of the compiler errors (and not a repeat question, as that question was already fully answered). I was asked whether I took a look at the errors, and to post the errors so that others might benefit from an understanding. This is the first error for this:

#include <bits/stdc++.h>
using namespace std;

struct foo {
  int n;
  foo(int n): n(n) {};
  // foo(): n(0) {};
};
int main(){
  unordered_map<int, pair<foo,foo>> m;
  m[3] = make_pair(foo(1),foo(2));
}

这是编译后的第一个错误(暂时省略):

And here is the first error after compilation (rest omitted for now):

g++ -std=c++17 -Weffc++ -Wall -Wextra -Wsign-conversion  pairs.cpp -o ../build/pairs.bin
In file included from /usr/include/c++/8/functional:54,
                 from /usr/include/x86_64-linux-gnu/c++/8/bits/stdc++.h:71,
                 from pairs.cpp:1:
/usr/include/c++/8/tuple: In instantiation of ‘std::pair<_T1, _T2>::pair(std::tuple<_Args1 ...>&, std::tuple<_Args2 ...>&, std::_Index_tuple<_Indexes1 ...>, std::_Index_tuple<_Indexes2 ...>) [with _Args1 = {int&&}; long unsigned int ..._Indexes1 = {0}; _Args2 = {}; long unsigned int ..._Indexes2 = {}; _T1 = const int; _T2 = std::pair<foo, foo>]’:
/usr/include/c++/8/tuple:1657:63:   required from ‘std::pair<_T1, _T2>::pair(std::piecewise_construct_t, std::tuple<_Args1 ...>, std::tuple<_Args2 ...>) [with _Args1 = {int&&}; _Args2 = {}; _T1 = const int; _T2 = std::pair<foo, foo>]’
/usr/include/c++/8/ext/new_allocator.h:136:4:   required from ‘void __gnu_cxx::new_allocator<_Tp>::construct(_Up*, _Args&& ...) [with _Up = std::pair<const int, std::pair<foo, foo> >; _Args = {const std::piecewise_construct_t&, std::tuple<int&&>, std::tuple<>}; _Tp = std::__detail::_Hash_node<std::pair<const int, std::pair<foo, foo> >, false>]’
/usr/include/c++/8/bits/alloc_traits.h:475:4:   required from ‘static void std::allocator_traits<std::allocator<_CharT> >::construct(std::allocator_traits<std::allocator<_CharT> >::allocator_type&, _Up*, _Args&& ...) [with _Up = std::pair<const int, std::pair<foo, foo> >; _Args = {const std::piecewise_construct_t&, std::tuple<int&&>, std::tuple<>}; _Tp = std::__detail::_Hash_node<std::pair<const int, std::pair<foo, foo> >, false>; std::allocator_traits<std::allocator<_CharT> >::allocator_type = std::allocator<std::__detail::_Hash_node<std::pair<const int, std::pair<foo, foo> >, false> >]’
/usr/include/c++/8/bits/hashtable_policy.h:2093:36:   required from ‘std::__detail::_Hashtable_alloc<_NodeAlloc>::__node_type* std::__detail::_Hashtable_alloc<_NodeAlloc>::_M_allocate_node(_Args&& ...) [with _Args = {const std::piecewise_construct_t&, std::tuple<int&&>, std::tuple<>}; _NodeAlloc = std::allocator<std::__detail::_Hash_node<std::pair<const int, std::pair<foo, foo> >, false> >; std::__detail::_Hashtable_alloc<_NodeAlloc>::__node_type = std::__detail::_Hash_node<std::pair<const int, std::pair<foo, foo> >, false>]’
/usr/include/c++/8/bits/hashtable_policy.h:736:8:   required from ‘std::__detail::_Map_base<_Key, _Pair, _Alloc, std::__detail::_Select1st, _Equal, _H1, _H2, _Hash, _RehashPolicy, _Traits, true>::mapped_type& std::__detail::_Map_base<_Key, _Pair, _Alloc, std::__detail::_Select1st, _Equal, _H1, _H2, _Hash, _RehashPolicy, _Traits, true>::operator[](std::__detail::_Map_base<_Key, _Pair, _Alloc, std::__detail::_Select1st, _Equal, _H1, _H2, _Hash, _RehashPolicy, _Traits, true>::key_type&&) [with _Key = int; _Pair = std::pair<const int, std::pair<foo, foo> >; _Alloc = std::allocator<std::pair<const int, std::pair<foo, foo> > >; _Equal = std::equal_to<int>; _H1 = std::hash<int>; _H2 = std::__detail::_Mod_range_hashing; _Hash = std::__detail::_Default_ranged_hash; _RehashPolicy = std::__detail::_Prime_rehash_policy; _Traits = std::__detail::_Hashtable_traits<false, false, true>; std::__detail::_Map_base<_Key, _Pair, _Alloc, std::__detail::_Select1st, _Equal, _H1, _H2, _Hash, _RehashPolicy, _Traits, true>::mapped_type = std::pair<foo, foo>; std::__detail::_Map_base<_Key, _Pair, _Alloc, std::__detail::_Select1st, _Equal, _H1, _H2, _Hash, _RehashPolicy, _Traits, true>::key_type = int]’
/usr/include/c++/8/bits/unordered_map.h:978:20:   required from ‘std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::mapped_type& std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::operator[](std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::key_type&&) [with _Key = int; _Tp = std::pair<foo, foo>; _Hash = std::hash<int>; _Pred = std::equal_to<int>; _Alloc = std::allocator<std::pair<const int, std::pair<foo, foo> > >; std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::mapped_type = std::pair<foo, foo>; std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::key_type = int]’
pairs.cpp:11:6:   required from here
/usr/include/c++/8/tuple:1668:70: error: no matching function for call to ‘std::pair<foo, foo>::pair()’
         second(std::forward<_Args2>(std::get<_Indexes2>(__tuple2))...)

推荐答案

通常尝试向后读取这些错误消息.

In general try to read these error message backwards.

1..查看最后一行(将其分解以更好地适合可用空间)

1. Look at the last line (broken up to fit better on the available space)

/usr/include/c++/8/tuple:1668:70: error: no matching function for call
         to ‘std::pair<foo, foo>::pair()’
         second(std::forward<_Args2>(std::get<_Indexes2>(__tuple2))...)

这立即告诉您发生了什么:有一个调用 somewhere (我们必须找出它的起源),该调用不存在.该功能称为

This tells you immediately what's going on: There is a call somewhere (we'll have to find out where it originates), to a function that doesn't exist. That function is called

std::pair<foo, foo>::pair()

这是标准库中某种类型的构造函数.具体来说,它是一个没有参数的构造函数.

This is a constructor of a type from the standard library. Specifically it is a constructor without arguments.

2..让我们看一下配对文件.奇怪的是,似乎有一个没有参数的构造函数.

2. So let's have a look at the documentation for pair. Oddly enough, there seems to be a constructor without arguments there.

3.为什么不存在?让我们在文档中继续阅读:

3. Why doesn't it exist? Let's read on in the documentation:

1)默认构造函数.值初始化对中的两个元素,第一和第二.
仅当 std :: is_default_constructible_v< first_type> 并且 std :: is_default_constructible_v< second_type> 都为真.当且仅当first_type或second_type不能隐式默认构造时,此构造函数才是显式的.

1) Default constructor. Value-initializes both elements of the pair, first and second.
This constructor participates in overload resolution if and only if std::is_default_constructible_v<first_type> and std::is_default_constructible_v<second_type> are both true. This constructor is explicit if and only if either first_type or second_type is not implicitly default-constructible.

(重点是我的)

4.好了,我们检查一下条件,在这种情况下, first_type second_type 都是 foo 类型.让我们检查一下这种类型:

4. Great let's check the condition, first_type and second_type are both the type foo in this case. Let's examine this type:

struct foo {
  int n;
  foo(int n): n(n) {};
  // foo(): n(0) {};
};

有一个用于此类型的 foo(int)构造函数,以及一个注释掉的 foo()构造函数.应该有一个隐式的默认构造函数,对吗? 否:

There is a foo(int) constructor for this type, and a commented out foo() constructor. There should be an implicit default constructor, right? No:

如果没有为类提供用户声明的任何类型的构造函数类型(结构,类或联合),编译器将始终声明一个默认构造函数作为其类的嵌入式公共成员.

If no user-declared constructors of any kind are provided for a class type (struct, class, or union), the compiler will always declare a default constructor as an inline public member of its class.

我们有一个 foo(int),因此编译器不会自行生成 foo().啊哈,调用失败,因为确实不存在该功能.

We have a foo(int), hence the compiler does not generate foo() on its own. Aha, the call fails because indeed the function does not exist.

5.好的,但是为什么首先要调用它?我不记得调用过这样的功能.->在您的错误中查看下一页":

5. Okay, but why is it called in the first place? I don't remember calling such a function. -> Look at the "next" line in your error:

pairs.cpp:11:6:   required from here

该行(pairs.cpp中的第11行)显示为:

That line (line 11 in pairs.cpp) reads:

m[3] = make_pair(foo(1),foo(2));

6..文档中关于第一个调用的函数有什么要说的? std :: unordered_map< int,std :: pair< foo,foo>> :: operator [] :

6. What does the documentation have to say about the first function called there? std::unordered_map<int,std::pair<foo,foo>>::operator[]:

返回对映射到等效于以下项的键的值的引用键,如果该键尚不存在,请执行插入....使用默认分配器时,这将导致密钥被复制由键和构造的映射值,值初始化 .

Returns a reference to the value that is mapped to a key equivalent to key, performing an insertion if such key does not already exist. ... When the default allocator is used, this results in the key being copy constructed from key and the mapped value being value-initialized.

7..值初始化是什么意思?

7. What does value-initialisation mean?

T()     (1)     
new T ()    (2)     
Class::Class(...) : member() { ... }    (3)     
T object {};    (4)     (since C++11)
T{}     (5)     (since C++11)
new T {}    (6)     (since C++11)
Class::Class(...) : member{} { ... }    (7)     (since C++11)

请记住, T 是您的 unordered_map value_type ,而该代码又是 pair< foo,foo> .看起来可疑的是编译器之前找不到的调用.

Remember, T here is the value_type of your unordered_map, which in turn is pair<foo,foo>. That looks suspiciously like the call the compiler couldn't find earlier.

8.如果我们想使用 std :: unordered_map< K,T> :: operator [] T 最好是有价值的可初始化的,例如通过提供一个 T :: T()构造函数.那是我们必须修复它的地方.

8. If we want to use std::unordered_map<K,T>::operator[], T better be value initialisable, e.g. by providing a T::T() constructor. That's where we have to fix it.

结论

仔细阅读错误,查看文档中尚不了解的内容,并跟踪所用功能的要求.并记住向后阅读错误消息,以找出在哪里遇到错误,以及在哪个您自己的文件中触发了该错误.然后检查那条线.

Read the error meticulously, check out the documentation for stuff you don't already know and follow up on the requirements of the functions you are using. And remember to read the error message backwards to figure out where the error was encountered, and in which of your own files it was triggered. Then examine that line.

这篇关于解析编译错误:没有匹配函数可调用'std :: pair&lt;,&gt; :: pair()'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆