c++ - 优先级队列库中的 C++ 推送功能在我的环境中不起作用
问题描述
我试图解决一个竞争性编程问题(在 AtCoder 上),我想使用优先级队列,但后来我遇到了一些问题。
我写了下面的代码,但是当我尝试编译它时,我收到了错误消息,上面写着“没有匹配的成员函数调用'push'”(整个错误消息如下所示)
我不知道为什么会这样,我在 AtCoder Code Test 在线法官上进行了尝试。
https://atcoder.jp/contests/abc176/custom_test
它在在线法官上工作得很好。
除此之外,我和我的朋友谈到了这个问题,他说代码在他的环境中运行良好(他使用 Windows)。
这是代码。
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <sstream>
#include <set>
#include <map>
#include <vector>
#include <list>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <string>
#include <stack>
#include <queue>
#include <bitset>
#include <cassert>
#include <iomanip>
#include <ctime>
#include <complex>
using namespace std;
int main()
{
priority_queue<array<int, 3>> pq;
pq.push({0, 1, 4});
}
我编写了代码并使用 vscode(版本:1.49.1)构建它,这是我当时收到的错误消息。
> Executing task: g++ -std=c++14 -g -O2 abc176_d_11.cpp -o abc176_d_11 <
abc176_d_11.cpp:27:8: error: no matching member function for call to 'push'
pq.push({0, 1, 4});
~~~^~~~
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/queue:536:10: note: candidate function not viable: cannot convert initializer list argument to
'const std::__1::priority_queue<std::__1::array<int, 3>, std::__1::vector<std::__1::array<int, 3>, std::__1::allocator<std::__1::array<int, 3> > >,
std::__1::less<std::__1::array<int, 3> > >::value_type' (aka 'const std::__1::array<int, 3>')
void push(const value_type& __v);
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/queue:539:10: note: candidate function not viable: cannot convert initializer list argument to
'std::__1::priority_queue<std::__1::array<int, 3>, std::__1::vector<std::__1::array<int, 3>, std::__1::allocator<std::__1::array<int, 3> > >,
std::__1::less<std::__1::array<int, 3> > >::value_type' (aka 'std::__1::array<int, 3>')
void push(value_type&& __v);
^
In file included from abc176_d_11.cpp:8:
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/vector:426:68: error: implicit instantiation of undefined template 'std::__1::array<int, 3>'
__alloc_traits::destroy(__alloc(), _VSTD::__to_raw_pointer(--__soon_to_be_end));
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/vector:369:29: note: in instantiation of member function
'std::__1::__vector_base<std::__1::array<int, 3>, std::__1::allocator<std::__1::array<int, 3> > >::__destruct_at_end' requested here
void clear() _NOEXCEPT {__destruct_at_end(__begin_);}
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/vector:463:9: note: in instantiation of member function
'std::__1::__vector_base<std::__1::array<int, 3>, std::__1::allocator<std::__1::array<int, 3> > >::clear' requested here
clear();
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/vector:495:5: note: in instantiation of member function
'std::__1::__vector_base<std::__1::array<int, 3>, std::__1::allocator<std::__1::array<int, 3> > >::~__vector_base' requested here
vector() _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/queue:447:11: note: in instantiation of member function 'std::__1::vector<std::__1::array<int,
3>, std::__1::allocator<std::__1::array<int, 3> > >::vector' requested here
: c(), comp() {}
^
abc176_d_11.cpp:26:35: note: in instantiation of member function 'std::__1::priority_queue<std::__1::array<int, 3>, std::__1::vector<std::__1::array<int,
3>, std::__1::allocator<std::__1::array<int, 3> > >, std::__1::less<std::__1::array<int, 3> > >::priority_queue' requested here
priority_queue<array<int, 3>> pq;
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/__tuple:219:64: note: template is declared here
template <class _Tp, size_t _Size> struct _LIBCPP_TEMPLATE_VIS array;
^
In file included from abc176_d_11.cpp:3:
In file included from /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/iostream:37:
In file included from /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/ios:215:
In file included from /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/__locale:14:
In file included from /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/string:504:
In file included from /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/string_view:175:
In file included from /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/__string:56:
In file included from /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/algorithm:643:
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/memory:1816:55: error: implicit instantiation of undefined template 'std::__1::array<int, 3>'
{_VSTD::__libcpp_deallocate((void*)__p, __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));}
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/memory:1554:14: note: in instantiation of member function
'std::__1::allocator<std::__1::array<int, 3> >::deallocate' requested here
{__a.deallocate(__p, __n);}
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/vector:464:25: note: in instantiation of member function
'std::__1::allocator_traits<std::__1::allocator<std::__1::array<int, 3> > >::deallocate' requested here
__alloc_traits::deallocate(__alloc(), __begin_, capacity());
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/vector:495:5: note: in instantiation of member function
'std::__1::__vector_base<std::__1::array<int, 3>, std::__1::allocator<std::__1::array<int, 3> > >::~__vector_base' requested here
vector() _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/queue:447:11: note: in instantiation of member function 'std::__1::vector<std::__1::array<int,
3>, std::__1::allocator<std::__1::array<int, 3> > >::vector' requested here
: c(), comp() {}
^
abc176_d_11.cpp:26:35: note: in instantiation of member function 'std::__1::priority_queue<std::__1::array<int, 3>, std::__1::vector<std::__1::array<int,
3>, std::__1::allocator<std::__1::array<int, 3> > >, std::__1::less<std::__1::array<int, 3> > >::priority_queue' requested here
priority_queue<array<int, 3>> pq;
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/__tuple:219:64: note: template is declared here
template <class _Tp, size_t _Size> struct _LIBCPP_TEMPLATE_VIS array;
^
In file included from abc176_d_11.cpp:8:
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/vector:372:52: error: implicit instantiation of undefined template 'std::__1::array<int, 3>'
{return static_cast<size_type>(__end_cap() - __begin_);}
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/vector:464:57: note: in instantiation of member function
'std::__1::__vector_base<std::__1::array<int, 3>, std::__1::allocator<std::__1::array<int, 3> > >::capacity' requested here
__alloc_traits::deallocate(__alloc(), __begin_, capacity());
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/vector:495:5: note: in instantiation of member function
'std::__1::__vector_base<std::__1::array<int, 3>, std::__1::allocator<std::__1::array<int, 3> > >::~__vector_base' requested here
vector() _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/queue:447:11: note: in instantiation of member function 'std::__1::vector<std::__1::array<int,
3>, std::__1::allocator<std::__1::array<int, 3> > >::vector' requested here
: c(), comp() {}
^
abc176_d_11.cpp:26:35: note: in instantiation of member function 'std::__1::priority_queue<std::__1::array<int, 3>, std::__1::vector<std::__1::array<int,
3>, std::__1::allocator<std::__1::array<int, 3> > >, std::__1::less<std::__1::array<int, 3> > >::priority_queue' requested here
priority_queue<array<int, 3>> pq;
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/__tuple:219:64: note: template is declared here
template <class _Tp, size_t _Size> struct _LIBCPP_TEMPLATE_VIS array;
^
In file included from abc176_d_11.cpp:8:
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/vector:872:54: error: implicit instantiation of undefined template 'std::__1::array<int, 3>'
__annotate_contiguous_container(data(), data() + capacity(),
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/vector:551:9: note: in instantiation of member function 'std::__1::vector<std::__1::array<int,
3>, std::__1::allocator<std::__1::array<int, 3> > >::__annotate_delete' requested here
__annotate_delete();
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/queue:427:28: note: in instantiation of member function 'std::__1::vector<std::__1::array<int,
3>, std::__1::allocator<std::__1::array<int, 3> > >::~vector' requested here
class _LIBCPP_TEMPLATE_VIS priority_queue
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/__tuple:219:64: note: template is declared here
template <class _Tp, size_t _Size> struct _LIBCPP_TEMPLATE_VIS array;
^
5 errors generated.
The terminal process "/usr/local/bin/bash '-c', 'g++ -std=c++14 -g -O2 abc176_d_11.cpp -o abc176_d_11'" terminated with exit code: 1.
Terminal will be reused by tasks, press any key to close it.
之后,我尝试从普通(我的意思是,不在 vscode 中)终端(我使用 iTerm)编译它,但得到了类似的错误消息。这是我尝试在终端上编译代码时收到的错误消息。
[my_computer_name]@MacBook-Pro:~/coder$ g++ abc176_d_11.cpp
abc176_d_11.cpp:27:8: error: no matching member function for call to 'push'
pq.push({0, 1, 4});
~~~^~~~
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/queue:536:10: note: candidate function
not viable: cannot convert initializer list argument to 'const
std::__1::priority_queue<std::__1::array<int, 3>, std::__1::vector<std::__1::array<int, 3>,
std::__1::allocator<std::__1::array<int, 3> > >, std::__1::less<std::__1::array<int, 3> >
>::value_type' (aka 'const std::__1::array<int, 3>')
void push(const value_type& __v);
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/queue:539:10: note: candidate function
not viable: cannot convert initializer list argument to
'std::__1::priority_queue<std::__1::array<int, 3>, std::__1::vector<std::__1::array<int, 3>,
std::__1::allocator<std::__1::array<int, 3> > >, std::__1::less<std::__1::array<int, 3> >
>::value_type' (aka 'std::__1::array<int, 3>')
void push(value_type&& __v);
^
In file included from abc176_d_11.cpp:8:
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/vector:426:68: error: implicit
instantiation of undefined template 'std::__1::array<int, 3>'
__alloc_traits::destroy(__alloc(), _VSTD::__to_raw_pointer(--__soon_to_be_end));
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/vector:369:29: note: in instantiation of
member function 'std::__1::__vector_base<std::__1::array<int, 3>,
std::__1::allocator<std::__1::array<int, 3> > >::__destruct_at_end' requested here
void clear() _NOEXCEPT {__destruct_at_end(__begin_);}
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/vector:463:9: note: in instantiation of
member function 'std::__1::__vector_base<std::__1::array<int, 3>,
std::__1::allocator<std::__1::array<int, 3> > >::clear' requested here
clear();
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/vector:495:5: note: in instantiation of
member function 'std::__1::__vector_base<std::__1::array<int, 3>,
std::__1::allocator<std::__1::array<int, 3> > >::~__vector_base' requested here
vector() _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/queue:447:11: note: in instantiation of
member function 'std::__1::vector<std::__1::array<int, 3>,
std::__1::allocator<std::__1::array<int, 3> > >::vector' requested here
: c(), comp() {}
^
abc176_d_11.cpp:26:35: note: in instantiation of member function
'std::__1::priority_queue<std::__1::array<int, 3>, std::__1::vector<std::__1::array<int, 3>,
std::__1::allocator<std::__1::array<int, 3> > >, std::__1::less<std::__1::array<int, 3> >
>::priority_queue' requested here
priority_queue<array<int, 3>> pq;
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/__tuple:219:64: note: template is
declared here
template <class _Tp, size_t _Size> struct _LIBCPP_TEMPLATE_VIS array;
^
In file included from abc176_d_11.cpp:3:
In file included from /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/iostream:37:
In file included from /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/ios:215:
In file included from /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/__locale:14:
In file included from /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/string:504:
In file included from /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/string_view:175:
In file included from /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/__string:56:
In file included from /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/algorithm:643:
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/memory:1816:55: error: implicit
instantiation of undefined template 'std::__1::array<int, 3>'
{_VSTD::__libcpp_deallocate((void*)__p, __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));}
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/memory:1554:14: note: in instantiation
of member function 'std::__1::allocator<std::__1::array<int, 3> >::deallocate' requested here
{__a.deallocate(__p, __n);}
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/vector:464:25: note: in instantiation of
member function 'std::__1::allocator_traits<std::__1::allocator<std::__1::array<int, 3> >
>::deallocate' requested here
__alloc_traits::deallocate(__alloc(), __begin_, capacity());
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/vector:495:5: note: in instantiation of
member function 'std::__1::__vector_base<std::__1::array<int, 3>,
std::__1::allocator<std::__1::array<int, 3> > >::~__vector_base' requested here
vector() _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/queue:447:11: note: in instantiation of
member function 'std::__1::vector<std::__1::array<int, 3>,
std::__1::allocator<std::__1::array<int, 3> > >::vector' requested here
: c(), comp() {}
^
abc176_d_11.cpp:26:35: note: in instantiation of member function
'std::__1::priority_queue<std::__1::array<int, 3>, std::__1::vector<std::__1::array<int, 3>,
std::__1::allocator<std::__1::array<int, 3> > >, std::__1::less<std::__1::array<int, 3> >
>::priority_queue' requested here
priority_queue<array<int, 3>> pq;
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/__tuple:219:64: note: template is
declared here
template <class _Tp, size_t _Size> struct _LIBCPP_TEMPLATE_VIS array;
^
In file included from abc176_d_11.cpp:8:
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/vector:372:52: error: implicit
instantiation of undefined template 'std::__1::array<int, 3>'
{return static_cast<size_type>(__end_cap() - __begin_);}
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/vector:464:57: note: in instantiation of
member function 'std::__1::__vector_base<std::__1::array<int, 3>,
std::__1::allocator<std::__1::array<int, 3> > >::capacity' requested here
__alloc_traits::deallocate(__alloc(), __begin_, capacity());
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/vector:495:5: note: in instantiation of
member function 'std::__1::__vector_base<std::__1::array<int, 3>,
std::__1::allocator<std::__1::array<int, 3> > >::~__vector_base' requested here
vector() _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/queue:447:11: note: in instantiation of
member function 'std::__1::vector<std::__1::array<int, 3>,
std::__1::allocator<std::__1::array<int, 3> > >::vector' requested here
: c(), comp() {}
^
abc176_d_11.cpp:26:35: note: in instantiation of member function
'std::__1::priority_queue<std::__1::array<int, 3>, std::__1::vector<std::__1::array<int, 3>,
std::__1::allocator<std::__1::array<int, 3> > >, std::__1::less<std::__1::array<int, 3> >
>::priority_queue' requested here
priority_queue<array<int, 3>> pq;
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/__tuple:219:64: note: template is
declared here
template <class _Tp, size_t _Size> struct _LIBCPP_TEMPLATE_VIS array;
^
In file included from abc176_d_11.cpp:8:
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/vector:872:54: error: implicit
instantiation of undefined template 'std::__1::array<int, 3>'
__annotate_contiguous_container(data(), data() + capacity(),
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/vector:551:9: note: in instantiation of
member function 'std::__1::vector<std::__1::array<int, 3>,
std::__1::allocator<std::__1::array<int, 3> > >::__annotate_delete' requested here
__annotate_delete();
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/queue:427:28: note: in instantiation of
member function 'std::__1::vector<std::__1::array<int, 3>,
std::__1::allocator<std::__1::array<int, 3> > >::~vector' requested here
class _LIBCPP_TEMPLATE_VIS priority_queue
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/__tuple:219:64: note: template is
declared here
template <class _Tp, size_t _Size> struct _LIBCPP_TEMPLATE_VIS array;
^
5 errors generated.
这是我的环境。
MacBook Pro
macOS Catalina
Version 10.15.5
我仍然是编程的初学者,尤其是 C++,如果我的问题没有意义或看起来很愚蠢,我想提前道歉。我的英语也不好。
如果我能得到你的帮助,我将不胜感激。谢谢你。
解决方案
关键错误信息是这样的:
implicit instantiation of undefined template 'std::__1::array<int, 3>'
这表示array
未定义。因此,在您的环境中,您需要#include<array>
明确。
因此,最小的工作示例是这样的:
#include <array>
#include <queue>
using namespace std;
int main()
{
priority_queue<array<int, 3>> pq;
pq.push({0, 1, 4});
}
请注意,我明确包括<array>
和<queue>
(按字母顺序)。这是一个黄金标准:如果您使用库,请直接包含它,不要指望间接包含。在包含列表中保持一些顺序。不要包括你不需要的东西。
推荐阅读
- http - JW Player 无法在 https 上运行
- bash - 哪个触发了内部错误?
- php - 如何在 php 查询中使用 $_COOKIE?
- javascript - ES6 Promises 使用的浏览器特性是什么?
- xamarin.forms - 如何使用 Xamarin 表单使用 SAP Odata
- haskell - GHC 如何推断此 GADT 的预期类型?
- c# - 无法将当前 JSON 对象(例如 {"name":"value"})反序列化为 ASP.NET MVC 中的类型 'System.Collections.Generic.List`1
- java - 什么 Java TLS 操作会在握手时产生致命错误?
- javascript - 对象被复制,但我想要原件
- spring-boot - Spring Kafka 和主题消费者的数量