Hayasaka
Hayasaka

Reputation: 13

C++ push function in priority queue library won't work on my environment

i was trying to solve a competitive programming problem (on AtCoder) and i wanted to use priority queue, but then i got some problem.

i wrote the code below but when i tried to compile that, i got error messages that said "no matching member function for call to 'push' " (the entire error messages are shown below)

i have no idea why this is happening, and i tried it on AtCoder Code Test online judge.

https://atcoder.jp/contests/abc176/custom_test

it worked completely fine on the online judge.

adding to that, i talked about this problem with my friend and he said the code worked just fine on his environment (he uses Windows).

Here is the code.

#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});

}

i wrote the code and build it with vscode (Version: 1.49.1), and this is the error messages i got at that time.

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

after that i tried to compile it from normal (i mean, not within vscode) terminal (i used iTerm) but got kinda same sort of error messages. here is that error messages I got when I tried to compile the code on terminal.

[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.

Here is my environment.

MacBook Pro
macOS Catalina
Version 10.15.5

i'm still a beginner on programming, espicially c++, and if my question doesn't make sense or seems silly, i want to apologize in advance. my english is not good too.

i would be so grateful if i could have your help. thank you.

Upvotes: 1

Views: 743

Answers (2)

zkoza
zkoza

Reputation: 2861

The key error message is this:

implicit instantiation of undefined template 'std::__1::array<int, 3>'

This says that the array is undefined. So in your environment you need #include<array> explicitly.

The minimum working example is thus this:

#include <array>
#include <queue>

using namespace std;

int main()
{
    priority_queue<array<int, 3>> pq;
    pq.push({0, 1, 4});
}

Notice that I explicitly include <array> and <queue> (in alphabetical order). This is a golden standard: if you use a library, include it directly, do not count on indirect includes. Keep some order in you include list. Do not include what you need not.

Upvotes: 1

Chris Uzdavinis
Chris Uzdavinis

Reputation: 6131

Do you have an ancient version of g++? I tried compiling your code as far back as 4.8.1 (from 2013) and it worked in c++11 mode. In older versions than that, it didn't know some of the headers.
(Current version is 10.2.0)

I suggest you try playing with your code on Compiler Explorer. It gives you access to online versions of g++, clang, msvc++, and others, going back for many years over many versions, without any hassle of setup or installing anything. Here's your code with g++ 4.8:

https://godbolt.org/z/5TTjqd

Upvotes: 0

Related Questions