Why does the following code compile when using std::array but not when using std::vector:
using varianttypes = std::variant<int, double, std::unique_ptr<double>>;
std::array<varianttypes, 4> a{1, 2.2, 3, 4.3}; // compiles fine
std::vector<varianttypes> a{1, 2.2, 3, 4.3}; // gives an error
I get this error:
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1/__memory/allocator_traits.h:298:9: error: no matching function for call to 'construct_at'
_VSTD::construct_at(__p, _VSTD::forward<_Args>(__args)...);
^~~~~~~~~~~~~~~~~~~
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1/__config:858:15: note: expanded from macro '_VSTD'
#define _VSTD std::_LIBCPP_ABI_NAMESPACE
^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1/memory:750:18: note: in instantiation of function template specialization 'std::allocator_traits<std::allocator<std::variant<int, double, std::unique_ptr<double>>>>::construct<std::variant<int, double, std::unique_ptr<double>>, const std::variant<int, double, std::unique_ptr<double>> &, void, void>' requested here
_Traits::construct(__a, _VSTD::__to_address(__begin2), *__begin1);
^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1/vector:1099:12: note: in instantiation of function template specialization 'std::__construct_range_forward<std::allocator<std::variant<int, double, std::unique_ptr<double>>>, const std::variant<int, double, std::unique_ptr<double>> *, std::variant<int, double, std::unique_ptr<double>> *>' requested here
_VSTD::__construct_range_forward(this->__alloc(), __first, __last, __tx.__pos_);
^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1/vector:1357:9: note: in instantiation of function template specialization 'std::vector<std::variant<int, double, std::unique_ptr<double>>>::__construct_at_end<const std::variant<int, double, std::unique_ptr<double>> *>' requested here
__construct_at_end(__il.begin(), __il.end(), __il.size());
^
variantstuff.cpp:82:29: note: in instantiation of member function 'std::vector<std::variant<int, double, std::unique_ptr<double>>>::vector' requested here
std::vector<varianttypes> a{1, 2.2, 3, 4.3};
^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1/__memory/construct_at.h:35:16: note: candidate template ignored: substitution failure [with _Tp = std::variant<int, double, std::unique_ptr<double>>, _Args = <const std::variant<int, double, std::unique_ptr<double>> &>]: call to implicitly-deleted copy constructor of 'std::variant<int, double, std::unique_ptr<double>>'
constexpr _Tp* construct_at(_Tp* __location, _Args&& ...__args) {
^
1 error generated.
make: *** [variantstuff.o] Error 1
This question already has answers here:
Can std::begin work with array parameters and if so, how?
(5 answers)
Closed 1 year ago.
I have some simple code
#include<iterator>
int main() {
int y[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
auto a = std::begin(y);
std::cout << *a << std::endl;
return 0;
}
Which prints out 1 as expected.
However if I do this :
void checkNested(int val [10]) {
auto a = std::begin(val);
std::cout << *a << std::endl;
}
int main() {
int y[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
checkNested(y);
return 0;
}
I get compilation failures from both clang++ and g++.
From clang++ specifically I get:
auto a = std::begin(input);
^~~~~~~~~~
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/initializer_list:89:5: note: candidate template ignored: could not match 'initializer_list<type-parameter-0-0>' against 'int *'
begin(initializer_list<_Tp> __ils) noexcept
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/range_access.h:48:5: note: candidate template ignored: substitution failure [with _Container = int *]: member reference base type 'int *' is not a structure or union
begin(_Container& __cont) -> decltype(__cont.begin())
^ ~
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/range_access.h:58:5: note: candidate template ignored: substitution failure [with _Container = int *]: member reference base type 'int *const' is not a structure or union
begin(const _Container& __cont) -> decltype(__cont.begin())
^ ~
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/range_access.h:87:5: note: candidate template ignored: could not match '_Tp [_Nm]' against 'int *'
begin(_Tp (&__arr)[_Nm])
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/range_access.h:104:31: note: candidate template ignored: could not match 'valarray<type-parameter-0-0>' against 'int *'
template<typename _Tp> _Tp* begin(valarray<_Tp>&);
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/range_access.h:105:37: note: candidate template ignored: could not match 'valarray<type-parameter-0-0>' against 'int *'
template<typename _Tp> const _Tp* begin(const valarray<_Tp>&);
Just wanna know if there's something really obvious I'm missing here since I expected them to function the same.
Thanks
An array can't be passed by value, so your array decays into a pointer when passed to checkNested(), and std::begin() is not defined for a pointer, hence the error.
void checkNested(int val [10]) is just syntax sugar for void checkNested(int *val).
If you pass the array by reference instead, then the code will work:
void checkNested(int (&val) [10])
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.
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.
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
I am trying to compile the Eigen3 PartialPivLU example
MatrixXd A(2,2);
A << 2, -1, 1, 3;
PartialPivLU >> lu(A);
but I get compiler errors (see below).
If I remove the "Ref<> it compiles OK. Does anyone know how to use PartialPivLU with Ref<>?
Thanks
Steve
In file included from /apps/eigen/3.2.8/include/eigen3/Eigen/LU:23:0,
from /apps/eigen/3.2.8/include/eigen3/Eigen/Dense:2,
from test3.cc:2:
/apps/eigen/3.2.8/include/eigen3/Eigen/src/LU/PartialPivLU.h: In instantiation of 'class Eigen::PartialPivLU<Eigen::Ref<Eigen::Matrix<double, -1, -1> > >':
test3.cc:9:36: required from here
/apps/eigen/3.2.8/include/eigen3/Eigen/src/LU/PartialPivLU.h:52:10: error: 'Options' is not a member of 'Eigen::PartialPivLU<Eigen::Ref<Eigen::Matrix<double, -1, -1> > >::MatrixType {aka Eigen::Ref<Eigen::Matrix<double, -1, -1> >}'
/apps/eigen/3.2.8/include/eigen3/Eigen/src/LU/PartialPivLU.h: In instantiation of 'Eigen::PartialPivLU<MatrixType>::PartialPivLU(const MatrixType&) [with _MatrixType = Eigen::Ref<Eigen::Matrix<double, -1, -1> >; Eigen::PartialPivLU<MatrixType>::MatrixType = Eigen::Ref<Eigen::Matrix<double, -1, -1> >]':
test3.cc:9:36: required from here
/apps/eigen/3.2.8/include/eigen3/Eigen/src/LU/PartialPivLU.h:213:26: error: no matching function for call to 'Eigen::Ref<Eigen::Matrix<double, -1, -1> >::Ref(Eigen::MapBase<Eigen::Ref<Eigen::Matrix<double, -1, -1> >, 0>::Index, Eigen::MapBase<Eigen::Ref<Eigen::Matrix<double, -1, -1> >, 0>::Index)'
/apps/eigen/3.2.8/include/eigen3/Eigen/src/LU/PartialPivLU.h:213:26: note: candidates are:
In file included from /apps/eigen/3.2.8/include/eigen3/Eigen/Core:308:0,
from /apps/eigen/3.2.8/include/eigen3/Eigen/Dense:1,
from test3.cc:2:
/apps/eigen/3.2.8/include/eigen3/Eigen/src/Core/Ref.h:211:12: note: template<class Derived> Eigen::Ref::Ref(const Eigen::DenseBase<OtherDerived>&, typename Eigen::internal::enable_if<(bool)(typename Eigen::internal::traits<Eigen::Ref<_PlainObjectType, _Options, _StrideType> >::match<Derived>::MatchAtCompileTime), Derived>::type*)
/apps/eigen/3.2.8/include/eigen3/Eigen/src/Core/Ref.h:211:12: note: template argument deduction/substitution failed:
In file included from /apps/eigen/3.2.8/include/eigen3/Eigen/LU:23:0,
from /apps/eigen/3.2.8/include/eigen3/Eigen/Dense:2,
from test3.cc:2:
/apps/eigen/3.2.8/include/eigen3/Eigen/src/LU/PartialPivLU.h:213:26: note: mismatched types 'const Eigen::DenseBase<Derived>' and 'Eigen::MapBase<Eigen::Ref<Eigen::Matrix<double, -1, -1> >, 0>::Index {aka long int}'
In file included from /apps/eigen/3.2.8/include/eigen3/Eigen/Core:308:0,
from /apps/eigen/3.2.8/include/eigen3/Eigen/Dense:1,
from test3.cc:2:
/apps/eigen/3.2.8/include/eigen3/Eigen/src/Core/Ref.h:204:12: note: template<class Derived> Eigen::Ref::Ref(Eigen::PlainObjectBase<OtherDerived>&, typename Eigen::internal::enable_if<(bool)(typename Eigen::internal::traits<Eigen::Ref<_PlainObjectType, _Options, _StrideType> >::match<Derived>::MatchAtCompileTime), Derived>::type*)
/apps/eigen/3.2.8/include/eigen3/Eigen/src/Core/Ref.h:204:12: note: template argument deduction/substitution failed:
In file included from /apps/eigen/3.2.8/include/eigen3/Eigen/LU:23:0,
from /apps/eigen/3.2.8/include/eigen3/Eigen/Dense:2,
from test3.cc:2:
/apps/eigen/3.2.8/include/eigen3/Eigen/src/LU/PartialPivLU.h:213:26: note: mismatched types 'Eigen::PlainObjectBase<OtherDerived>' and 'Eigen::MapBase<Eigen::Ref<Eigen::Matrix<double, -1, -1> >, 0>::Index {aka long int}'
In file included from /apps/eigen/3.2.8/include/eigen3/Eigen/Core:308:0,
from /apps/eigen/3.2.8/include/eigen3/Eigen/Dense:1,
from test3.cc:2:
/apps/eigen/3.2.8/include/eigen3/Eigen/src/Core/Ref.h:194:12: note: template<class Derived> Eigen::Ref::Ref(const Eigen::PlainObjectBase<OtherDerived>&, typename Eigen::internal::enable_if<(bool)(typename Eigen::internal::traits<Eigen::Ref<_PlainObjectType, _Options, _StrideType> >::match<Derived>::MatchAtCompileTime), Derived>::type*)
/apps/eigen/3.2.8/include/eigen3/Eigen/src/Core/Ref.h:194:12: note: template argument deduction/substitution failed:
In file included from /apps/eigen/3.2.8/include/eigen3/Eigen/LU:23:0,
from /apps/eigen/3.2.8/include/eigen3/Eigen/Dense:2,
from test3.cc:2:
/apps/eigen/3.2.8/include/eigen3/Eigen/src/LU/PartialPivLU.h:213:26: note: mismatched types 'const Eigen::PlainObjectBase<OtherDerived>' and 'Eigen::MapBase<Eigen::Ref<Eigen::Matrix<double, -1, -1> >, 0>::Index {aka long int}'
In file included from /apps/eigen/3.2.8/include/eigen3/Eigen/Core:308:0,
from /apps/eigen/3.2.8/include/eigen3/Eigen/Dense:1,
from test3.cc:2:
/apps/eigen/3.2.8/include/eigen3/Eigen/src/Core/Ref.h:188:76: note: Eigen::Ref<Eigen::Matrix<double, -1, -1> >::Ref(const Eigen::Ref<Eigen::Matrix<double, -1, -1> >&)
/apps/eigen/3.2.8/include/eigen3/Eigen/src/Core/Ref.h:188:76: note: candidate expects 1 argument, 2 provided
As chtz noted, the problem was the version of Eigen I was using. You need to use Eigen 3.3 or above.