Предположим есть класс A
:
A.h
#include <vector>
#ifndef UN1_A_H
#define UN1_A_H
#include "B.h"
class A {
public:
std::vector<B&> get();
};
#endif //UN1_A_H
A.cpp
#include "A.h"
#include "B.h"
std::vector<B &> A::get() {
std::vector<B&> bs;
bs.push_back(*(new B()));
bs.push_back(*(new B()));
return bs;
}
И есть класс B
, в котором ничего нет.
Почему я не могу скомпилировать следующий код?:
#include "A.h"
#include "B.h"
int main() {
A& a = *(new A());
B& b = a.get()[0];
}
Вывод:
/usr/include/c++/7/bits/alloc_traits.h: In instantiation of ‘struct std::allocator_traits<std::allocator<B&> >’:
/usr/include/c++/7/ext/alloc_traits.h:50:10: required from ‘struct __gnu_cxx::__alloc_traits<std::allocator<B&> >’
/usr/include/c++/7/bits/stl_vector.h:77:21: required from ‘struct std::_Vector_base<B&, std::allocator<B&> >’
/usr/include/c++/7/bits/stl_vector.h:216:11: required from ‘class std::vector<B&>’
/home/wcobalt/Files/BoAaP/2/un1/A.cpp:8:25: required from here
/usr/include/c++/7/bits/alloc_traits.h:392:27: error: forming pointer to reference type ‘B&’
using pointer = _Tp*;
^
/usr/include/c++/7/bits/alloc_traits.h:395:39: error: forming pointer to reference type ‘B&’
using const_pointer = const _Tp*;
^
In file included from /usr/include/c++/7/bits/stl_construct.h:61:0,
from /usr/include/c++/7/vector:62,
from /home/wcobalt/Files/BoAaP/2/un1/A.h:5,
from /home/wcobalt/Files/BoAaP/2/un1/A.cpp:5:
/usr/include/c++/7/ext/alloc_traits.h: In instantiation of ‘struct __gnu_cxx::__alloc_traits<std::allocator<B&> >’:
/usr/include/c++/7/bits/stl_vector.h:77:21: required from ‘struct std::_Vector_base<B&, std::allocator<B&> >’
/usr/include/c++/7/bits/stl_vector.h:216:11: required from ‘class std::vector<B&>’
/home/wcobalt/Files/BoAaP/2/un1/A.cpp:8:25: required from here
/usr/include/c++/7/ext/alloc_traits.h:66:23: error: no members matching ‘__gnu_cxx::__alloc_traits<std::allocator<B&> >::_Base_type {aka std::allocator_traits<std::allocator<B&> >}::allocate’ in ‘__gnu_cxx::__alloc_traits<std::allocator<B&> >::_Base_type {aka struct std::allocator_traits<std::allocator<B&> >}’
using _Base_type::allocate;
^~~~~~~~
/usr/include/c++/7/ext/alloc_traits.h:67:23: error: no members matching ‘__gnu_cxx::__alloc_traits<std::allocator<B&> >::_Base_type {aka std::allocator_traits<std::allocator<B&> >}::deallocate’ in ‘__gnu_cxx::__alloc_traits<std::allocator<B&> >::_Base_type {aka struct std::allocator_traits<std::allocator<B&> >}’
using _Base_type::deallocate;
^~~~~~~~~~
In file included from /usr/include/x86_64-linux-gnu/c++/7/bits/c++allocator.h:33:0,
from /usr/include/c++/7/bits/allocator.h:46,
from /usr/include/c++/7/vector:61,
from /home/wcobalt/Files/BoAaP/2/un1/A.h:5,
from /home/wcobalt/Files/BoAaP/2/un1/A.cpp:5:
/usr/include/c++/7/ext/new_allocator.h: In instantiation of ‘class __gnu_cxx::new_allocator<B&>’:
/usr/include/c++/7/bits/allocator.h:108:11: required from ‘class std::allocator<B&>’
/usr/include/c++/7/bits/stl_vector.h:81:14: required from ‘struct std::_Vector_base<B&, std::allocator<B&> >::_Vector_impl’
/usr/include/c++/7/bits/stl_vector.h:166:20: required from ‘struct std::_Vector_base<B&, std::allocator<B&> >’
/usr/include/c++/7/bits/stl_vector.h:216:11: required from ‘class std::vector<B&>’
/home/wcobalt/Files/BoAaP/2/un1/A.cpp:8:25: required from here
/usr/include/c++/7/ext/new_allocator.h:63:26: error: forming pointer to reference type ‘B&’
typedef _Tp* pointer;
^~~~~~~
/usr/include/c++/7/ext/new_allocator.h:64:26: error: forming pointer to reference type ‘B&’
typedef const _Tp* const_pointer;
^~~~~~~~~~~~~
In file included from /usr/include/c++/7/vector:61:0,
from /home/wcobalt/Files/BoAaP/2/un1/A.h:5,
from /home/wcobalt/Files/BoAaP/2/un1/A.cpp:5:
/usr/include/c++/7/bits/allocator.h: In instantiation of ‘class std::allocator<B&>’:
/usr/include/c++/7/bits/stl_vector.h:81:14: required from ‘struct std::_Vector_base<B&, std::allocator<B&> >::_Vector_impl’
/usr/include/c++/7/bits/stl_vector.h:166:20: required from ‘struct std::_Vector_base<B&, std::allocator<B&> >’
/usr/include/c++/7/bits/stl_vector.h:216:11: required from ‘class std::vector<B&>’
/home/wcobalt/Files/BoAaP/2/un1/A.cpp:8:25: required from here
/usr/include/c++/7/bits/allocator.h:113:26: error: forming pointer to reference type ‘B&’
typedef _Tp* pointer;
^~~~~~~
/usr/include/c++/7/bits/allocator.h:114:26: error: forming pointer to reference type ‘B&’
typedef const _Tp* const_pointer;
^~~~~~~~~~~~~
In file included from /usr/include/c++/7/vector:64:0,
from /home/wcobalt/Files/BoAaP/2/un1/A.h:5,
from /home/wcobalt/Files/BoAaP/2/un1/A.cpp:5:
/usr/include/c++/7/bits/stl_vector.h: In instantiation of ‘class std::vector<B&>’:
/home/wcobalt/Files/BoAaP/2/un1/A.cpp:8:25: required from here
/usr/include/c++/7/bits/stl_vector.h:247:20: error: no members matching ‘std::vector<B&>::_Base {aka std::_Vector_base<B&, std::allocator<B&> >}::_M_allocate’ in ‘std::vector<B&>::_Base {aka struct std::_Vector_base<B&, std::allocator<B&> >}’
using _Base::_M_allocate;
^~~~~~~~~~~
/usr/include/c++/7/bits/stl_vector.h:248:20: error: no members matching ‘std::vector<B&>::_Base {aka std::_Vector_base<B&, std::allocator<B&> >}::_M_deallocate’ in ‘std::vector<B&>::_Base {aka struct std::_Vector_base<B&, std::allocator<B&> >}’
using _Base::_M_deallocate;
^~~~~~~~~~~~~
/usr/include/c++/7/bits/stl_vector.h:920:7: error: forming pointer to reference type ‘B&’
data() _GLIBCXX_NOEXCEPT
^~~~
/usr/include/c++/7/bits/stl_vector.h:924:7: error: forming pointer to reference type ‘B&’
data() const _GLIBCXX_NOEXCEPT
^~~~
/usr/include/c++/7/bits/stl_vector.h:953:7: error: ‘void std::vector<_Tp, _Alloc>::push_back(std::vector<_Tp, _Alloc>::value_type&&) [with _Tp = B&; _Alloc = std::allocator<B&>; std::vector<_Tp, _Alloc>::value_type = B&]’ cannot be overloaded
push_back(value_type&& __x)
^~~~~~~~~
/usr/include/c++/7/bits/stl_vector.h:939:7: error: with ‘void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = B&; _Alloc = std::allocator<B&>; std::vector<_Tp, _Alloc>::value_type = B&]’
push_back(const value_type& __x)
^~~~~~~~~
In file included from /usr/include/c++/7/bits/stl_vector.h:63:0,
from /usr/include/c++/7/vector:64,
from /home/wcobalt/Files/BoAaP/2/un1/A.h:5,
from /home/wcobalt/Files/BoAaP/2/un1/A.cpp:5:
/usr/include/c++/7/initializer_list: In instantiation of ‘class std::initializer_list<B&>’:
/home/wcobalt/Files/BoAaP/2/un1/A.cpp:14:12: required from here
/usr/include/c++/7/initializer_list:54:26: error: forming pointer to reference type ‘B&’
typedef const _E* iterator;
^~~~~~~~
/usr/include/c++/7/initializer_list:55:26: error: forming pointer to reference type ‘B&’
typedef const _E* const_iterator;
^~~~~~~~~~~~~~
In file included from /usr/include/c++/7/vector:64:0,
from /home/wcobalt/Files/BoAaP/2/un1/A.h:5,
from /home/wcobalt/Files/BoAaP/2/un1/A.cpp:5:
/usr/include/c++/7/bits/stl_vector.h: In instantiation of ‘std::vector<_Tp, _Alloc>::~vector() [with _Tp = B&; _Alloc = std::allocator<B&>]’:
/home/wcobalt/Files/BoAaP/2/un1/A.cpp:9:21: required from here
/usr/include/c++/7/bits/stl_vector.h:434:37: error: ‘struct std::_Vector_base<B&, std::allocator<B&> >::_Vector_impl’ has no member named ‘_M_start’; did you mean ‘_M_swap_data’?
{ std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
~~~~~~~~~~~~~~^~~~~~~~
_M_swap_data
/usr/include/c++/7/bits/stl_vector.h:434:61: error: ‘struct std::_Vector_base<B&, std::allocator<B&> >::_Vector_impl’ has no member named ‘_M_finish’
{ std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
~~~~~~~~~~~~~~^~~~~~~~~
/usr/include/c++/7/bits/stl_vector.h: In instantiation of ‘void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = B&; _Alloc = std::allocator<B&>; std::vector<_Tp, _Alloc>::value_type = B&]’:
/home/wcobalt/Files/BoAaP/2/un1/A.cpp:11:28: required from here
/usr/include/c++/7/bits/stl_vector.h:941:20: error: ‘struct std::_Vector_base<B&, std::allocator<B&> >::_Vector_impl’ has no member named ‘_M_finish’
if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage)
~~~~~~~~~~~~~~^~~~~~~~~
/usr/include/c++/7/bits/stl_vector.h:941:47: error: ‘struct std::_Vector_base<B&, std::allocator<B&> >::_Vector_impl’ has no member named ‘_M_end_of_storage’
if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage)
~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~
/usr/include/c++/7/bits/stl_vector.h:943:60: error: ‘struct std::_Vector_base<B&, std::allocator<B&> >::_Vector_impl’ has no member named ‘_M_finish’
_Alloc_traits::construct(this->_M_impl, this->_M_impl._M_finish,
~~~~~~~~~~~~~~^~~~~~~~~
/usr/include/c++/7/bits/stl_vector.h:945:22: error: ‘struct std::_Vector_base<B&, std::allocator<B&> >::_Vector_impl’ has no member named ‘_M_finish’
++this->_M_impl._M_finish;
~~~~~~~~~~~~~~^~~~~~~~~
/usr/include/c++/7/bits/stl_vector.h:948:25: error: no matching function for call to ‘end()’
_M_realloc_insert(end(), __x);
~~~^~
In file included from /usr/include/c++/7/bits/stl_vector.h:63:0,
from /usr/include/c++/7/vector:64,
from /home/wcobalt/Files/BoAaP/2/un1/A.h:5,
from /home/wcobalt/Files/BoAaP/2/un1/A.cpp:5:
/usr/include/c++/7/initializer_list:99:5: note: candidate: template<class _Tp> constexpr const _Tp* std::end(std::initializer_list<_Tp>)
end(initializer_list<_Tp> __ils) noexcept
^~~
/usr/include/c++/7/initializer_list:99:5: note: template argument deduction/substitution failed:
In file included from /usr/include/c++/7/vector:64:0,
from /home/wcobalt/Files/BoAaP/2/un1/A.h:5,
from /home/wcobalt/Files/BoAaP/2/un1/A.cpp:5:
/usr/include/c++/7/bits/stl_vector.h:948:25: note: candidate expects 1 argument, 0 provided
_M_realloc_insert(end(), __x);
~~~^~
In file included from /usr/include/c++/7/vector:66:0,
from /home/wcobalt/Files/BoAaP/2/un1/A.h:5,
from /home/wcobalt/Files/BoAaP/2/un1/A.cpp:5:
/usr/include/c++/7/bits/range_access.h:68:5: note: candidate: template<class _Container> decltype (__cont.end()) std::end(_Container&)
end(_Container& __cont) -> decltype(__cont.end())
^~~
/usr/include/c++/7/bits/range_access.h:68:5: note: template argument deduction/substitution failed:
In file included from /usr/include/c++/7/vector:64:0,
from /home/wcobalt/Files/BoAaP/2/un1/A.h:5,
from /home/wcobalt/Files/BoAaP/2/un1/A.cpp:5:
/usr/include/c++/7/bits/stl_vector.h:948:25: note: candidate expects 1 argument, 0 provided
_M_realloc_insert(end(), __x);
~~~^~
In file included from /usr/include/c++/7/vector:66:0,
from /home/wcobalt/Files/BoAaP/2/un1/A.h:5,
from /home/wcobalt/Files/BoAaP/2/un1/A.cpp:5:
/usr/include/c++/7/bits/range_access.h:78:5: note: candidate: template<class _Container> decltype (__cont.end()) std::end(const _Container&)
end(const _Container& __cont) -> decltype(__cont.end())
^~~
/usr/include/c++/7/bits/range_access.h:78:5: note: template argument deduction/substitution failed:
In file included from /usr/include/c++/7/vector:64:0,
from /home/wcobalt/Files/BoAaP/2/un1/A.h:5,
from /home/wcobalt/Files/BoAaP/2/un1/A.cpp:5:
/usr/include/c++/7/bits/stl_vector.h:948:25: note: candidate expects 1 argument, 0 provided
_M_realloc_insert(end(), __x);
~~~^~
In file included from /usr/include/c++/7/vector:66:0,
from /home/wcobalt/Files/BoAaP/2/un1/A.h:5,
from /home/wcobalt/Files/BoAaP/2/un1/A.cpp:5:
/usr/include/c++/7/bits/range_access.h:97:5: note: candidate: template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::end(_Tp (&)[_Nm])
end(_Tp (&__arr)[_Nm])
^~~
/usr/include/c++/7/bits/range_access.h:97:5: note: template argument deduction/substitution failed:
In file included from /usr/include/c++/7/vector:64:0,
from /home/wcobalt/Files/BoAaP/2/un1/A.h:5,
from /home/wcobalt/Files/BoAaP/2/un1/A.cpp:5:
/usr/include/c++/7/bits/stl_vector.h:948:25: note: candidate expects 1 argument, 0 provided
_M_realloc_insert(end(), __x);
~~~^~
In file included from /usr/include/c++/7/vector:66:0,
from /home/wcobalt/Files/BoAaP/2/un1/A.h:5,
from /home/wcobalt/Files/BoAaP/2/un1/A.cpp:5:
/usr/include/c++/7/bits/range_access.h:106:31: note: candidate: template<class _Tp> _Tp* std::end(std::valarray<_Tp>&)
template<typename _Tp> _Tp* end(valarray<_Tp>&);
^~~
/usr/include/c++/7/bits/range_access.h:106:31: note: template argument deduction/substitution failed:
In file included from /usr/include/c++/7/vector:64:0,
from /home/wcobalt/Files/BoAaP/2/un1/A.h:5,
from /home/wcobalt/Files/BoAaP/2/un1/A.cpp:5:
/usr/include/c++/7/bits/stl_vector.h:948:25: note: candidate expects 1 argument, 0 provided
_M_realloc_insert(end(), __x);
~~~^~
In file included from /usr/include/c++/7/vector:66:0,
from /home/wcobalt/Files/BoAaP/2/un1/A.h:5,
from /home/wcobalt/Files/BoAaP/2/un1/A.cpp:5:
/usr/include/c++/7/bits/range_access.h:107:37: note: candidate: template<class _Tp> const _Tp* std::end(const std::valarray<_Tp>&)
template<typename _Tp> const _Tp* end(const valarray<_Tp>&);
^~~
/usr/include/c++/7/bits/range_access.h:107:37: note: template argument deduction/substitution failed:
In file included from /usr/include/c++/7/vector:64:0,
from /home/wcobalt/Files/BoAaP/2/un1/A.h:5,
from /home/wcobalt/Files/BoAaP/2/un1/A.cpp:5:
/usr/include/c++/7/bits/stl_vector.h:948:25: note: candidate expects 1 argument, 0 provided
_M_realloc_insert(end(), __x);
~~~^~
/usr/include/c++/7/bits/stl_vector.h:948:21: error: ‘_M_realloc_insert’ was not declared in this scope
_M_realloc_insert(end(), __x);
~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
/usr/include/c++/7/bits/stl_vector.h: In instantiation of ‘std::_Vector_base<_Tp, _Alloc>::~_Vector_base() [with _Tp = B&; _Alloc = std::allocator<B&>]’:
/usr/include/c++/7/bits/stl_vector.h:263:15: required from ‘std::vector<_Tp, _Alloc>::vector() [with _Tp = B&; _Alloc = std::allocator<B&>]’
/home/wcobalt/Files/BoAaP/2/un1/A.cpp:9:21: required from here
/usr/include/c++/7/bits/stl_vector.h:162:37: error: ‘struct std::_Vector_base<B&, std::allocator<B&> >::_Vector_impl’ has no member named ‘_M_start’; did you mean ‘_M_swap_data’?
{ _M_deallocate(this->_M_impl._M_start, this->_M_impl._M_end_of_storage
~~~~~~~~~~~~~~^~~~~~~~
_M_swap_data
/usr/include/c++/7/bits/stl_vector.h:162:61: error: ‘struct std::_Vector_base<B&, std::allocator<B&> >::_Vector_impl’ has no member named ‘_M_end_of_storage’
{ _M_deallocate(this->_M_impl._M_start, this->_M_impl._M_end_of_storage
~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~
/usr/include/c++/7/bits/stl_vector.h:163:25: error: ‘struct std::_Vector_base<B&, std::allocator<B&> >::_Vector_impl’ has no member named ‘_M_start’; did you mean ‘_M_swap_data’?
- this->_M_impl._M_start); }
~~~~~~~~~~~~~~^~~~~~~~
_M_swap_data
/usr/include/c++/7/bits/stl_vector.h:162:22: error: ‘_M_deallocate’ was not declared in this scope
{ _M_deallocate(this->_M_impl._M_start, this->_M_impl._M_end_of_storage
~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- this->_M_impl._M_start); }
~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/7/bits/stl_vector.h: In instantiation of ‘std::_Vector_base<_Tp, _Alloc>::_Vector_impl::_Vector_impl() [with _Tp = B&; _Alloc = std::allocator<B&>]’:
/usr/include/c++/7/bits/stl_vector.h:127:17: required from ‘std::_Vector_base<_Tp, _Alloc>::_Vector_base() [with _Tp = B&; _Alloc = std::allocator<B&>]’
/usr/include/c++/7/bits/stl_vector.h:263:15: required from ‘std::vector<_Tp, _Alloc>::vector() [with _Tp = B&; _Alloc = std::allocator<B&>]’
/home/wcobalt/Files/BoAaP/2/un1/A.cpp:9:21: required from here
/usr/include/c++/7/bits/stl_vector.h:89:65: error: using invalid field ‘std::_Vector_base<_Tp, _Alloc>::_Vector_impl::_M_start’
: _Tp_alloc_type(), _M_start(), _M_finish(), _M_end_of_storage()
^
/usr/include/c++/7/bits/stl_vector.h:89:65: error: using invalid field ‘std::_Vector_base<_Tp, _Alloc>::_Vector_impl::_M_finish’
/usr/include/c++/7/bits/stl_vector.h:89:65: error: using invalid field ‘std::_Vector_base<_Tp, _Alloc>::_Vector_impl::_M_end_of_storage’
/usr/include/c++/7/bits/stl_vector.h: In instantiation of ‘std::_Vector_base<_Tp, _Alloc>::_Vector_impl::_Vector_impl(std::_Vector_base<_Tp, _Alloc>::_Tp_alloc_type&&) [with _Tp = B&; _Alloc = std::allocator<B&>; std::_Vector_base<_Tp, _Alloc>::_Tp_alloc_type = std::allocator<B&>]’:
/usr/include/c++/7/bits/stl_vector.h:145:53: required from ‘std::_Vector_base<_Tp, _Alloc>::_Vector_base(std::_Vector_base<_Tp, _Alloc>&&) [with _Tp = B&; _Alloc = std::allocator<B&>]’
/usr/include/c++/7/bits/stl_vector.h:345:29: required from ‘std::vector<_Tp, _Alloc>::vector(std::vector<_Tp, _Alloc>&&) [with _Tp = B&; _Alloc = std::allocator<B&>]’
/home/wcobalt/Files/BoAaP/2/un1/A.cpp:14:12: required from here
/usr/include/c++/7/bits/stl_vector.h:99:47: error: using invalid field ‘std::_Vector_base<_Tp, _Alloc>::_Vector_impl::_M_start’
_M_start(), _M_finish(), _M_end_of_storage()
^
/usr/include/c++/7/bits/stl_vector.h:99:47: error: using invalid field ‘std::_Vector_base<_Tp, _Alloc>::_Vector_impl::_M_finish’
/usr/include/c++/7/bits/stl_vector.h:99:47: error: using invalid field ‘std::_Vector_base<_Tp, _Alloc>::_Vector_impl::_M_end_of_storage’
/usr/include/c++/7/bits/stl_vector.h: In instantiation of ‘void std::_Vector_base<_Tp, _Alloc>::_Vector_impl::_M_swap_data(std::_Vector_base<_Tp, _Alloc>::_Vector_impl&) [with _Tp = B&; _Alloc = std::allocator<B&>]’:
/usr/include/c++/7/bits/stl_vector.h:146:9: required from ‘std::_Vector_base<_Tp, _Alloc>::_Vector_base(std::_Vector_base<_Tp, _Alloc>&&) [with _Tp = B&; _Alloc = std::allocator<B&>]’
/usr/include/c++/7/bits/stl_vector.h:345:29: required from ‘std::vector<_Tp, _Alloc>::vector(std::vector<_Tp, _Alloc>&&) [with _Tp = B&; _Alloc = std::allocator<B&>]’
/home/wcobalt/Files/BoAaP/2/un1/A.cpp:14:12: required from here
/usr/include/c++/7/bits/stl_vector.h:105:14: error: using invalid field ‘std::_Vector_base<_Tp, _Alloc>::_Vector_impl::_M_start’
std::swap(_M_start, __x._M_start);
^~~~~~~~
/usr/include/c++/7/bits/stl_vector.h:105:28: error: ‘struct std::_Vector_base<B&, std::allocator<B&> >::_Vector_impl’ has no member named ‘_M_start’; did you mean ‘_M_swap_data’?
std::swap(_M_start, __x._M_start);
~~~~^~~~~~~~
_M_swap_data
/usr/include/c++/7/bits/stl_vector.h:106:14: error: using invalid field ‘std::_Vector_base<_Tp, _Alloc>::_Vector_impl::_M_finish’
std::swap(_M_finish, __x._M_finish);
^~~~~~~~~
/usr/include/c++/7/bits/stl_vector.h:106:29: error: ‘struct std::_Vector_base<B&, std::allocator<B&> >::_Vector_impl’ has no member named ‘_M_finish’
std::swap(_M_finish, __x._M_finish);
~~~~^~~~~~~~~
/usr/include/c++/7/bits/stl_vector.h:107:14: error: using invalid field ‘std::_Vector_base<_Tp, _Alloc>::_Vector_impl::_M_end_of_storage’
std::swap(_M_end_of_storage, __x._M_end_of_storage);
^~~~~~~~~~~~~~~~~
/usr/include/c++/7/bits/stl_vector.h:107:37: error: ‘struct std::_Vector_base<B&, std::allocator<B&> >::_Vector_impl’ has no member named ‘_M_end_of_storage’
std::swap(_M_end_of_storage, __x._M_end_of_storage);
~~~~^~~~~~~~~~~~~~~~~
CMakeFiles/un1.dir/build.make:75: recipe for target 'CMakeFiles/un1.dir/A.cpp.o' failed
make[3]: *** [CMakeFiles/un1.dir/A.cpp.o] Error 1
make[3]: *** Waiting for unfinished jobs....
In file included from /usr/include/c++/7/ext/alloc_traits.h:36:0,
from /usr/include/c++/7/bits/basic_string.h:40,
from /usr/include/c++/7/string:52,
from /usr/include/c++/7/bits/locale_classes.h:40,
from /usr/include/c++/7/bits/ios_base.h:41,
from /usr/include/c++/7/ios:42,
from /usr/include/c++/7/ostream:38,
from /usr/include/c++/7/iostream:39,
from /home/wcobalt/Files/BoAaP/2/un1/main.cpp:1:
/usr/include/c++/7/bits/alloc_traits.h: In instantiation of ‘struct std::allocator_traits<std::allocator<B&> >’:
/usr/include/c++/7/ext/alloc_traits.h:50:10: required from ‘struct __gnu_cxx::__alloc_traits<std::allocator<B&> >’
/usr/include/c++/7/bits/stl_vector.h:77:21: required from ‘struct std::_Vector_base<B&, std::allocator<B&> >’
/usr/include/c++/7/bits/stl_vector.h:216:11: required from ‘class std::vector<B&>’
/home/wcobalt/Files/BoAaP/2/un1/main.cpp:7:18: required from here
/usr/include/c++/7/bits/alloc_traits.h:392:27: error: forming pointer to reference type ‘B&’
using pointer = _Tp*;
^
/usr/include/c++/7/bits/alloc_traits.h:395:39: error: forming pointer to reference type ‘B&’
using const_pointer = const _Tp*;
^
In file included from /usr/include/c++/7/bits/basic_string.h:40:0,
from /usr/include/c++/7/string:52,
from /usr/include/c++/7/bits/locale_classes.h:40,
from /usr/include/c++/7/bits/ios_base.h:41,
from /usr/include/c++/7/ios:42,
from /usr/include/c++/7/ostream:38,
from /usr/include/c++/7/iostream:39,
from /home/wcobalt/Files/BoAaP/2/un1/main.cpp:1:
/usr/include/c++/7/ext/alloc_traits.h: In instantiation of ‘struct __gnu_cxx::__alloc_traits<std::allocator<B&> >’:
/usr/include/c++/7/bits/stl_vector.h:77:21: required from ‘struct std::_Vector_base<B&, std::allocator<B&> >’
/usr/include/c++/7/bits/stl_vector.h:216:11: required from ‘class std::vector<B&>’
/home/wcobalt/Files/BoAaP/2/un1/main.cpp:7:18: required from here
/usr/include/c++/7/ext/alloc_traits.h:66:23: error: no members matching ‘__gnu_cxx::__alloc_traits<std::allocator<B&> >::_Base_type {aka std::allocator_traits<std::allocator<B&> >}::allocate’ in ‘__gnu_cxx::__alloc_traits<std::allocator<B&> >::_Base_type {aka struct std::allocator_traits<std::allocator<B&> >}’
using _Base_type::allocate;
^~~~~~~~
/usr/include/c++/7/ext/alloc_traits.h:67:23: error: no members matching ‘__gnu_cxx::__alloc_traits<std::allocator<B&> >::_Base_type {aka std::allocator_traits<std::allocator<B&> >}::deallocate’ in ‘__gnu_cxx::__alloc_traits<std::allocator<B&> >::_Base_type {aka struct std::allocator_traits<std::allocator<B&> >}’
using _Base_type::deallocate;
^~~~~~~~~~
In file included from /usr/include/x86_64-linux-gnu/c++/7/bits/c++allocator.h:33:0,
from /usr/include/c++/7/bits/allocator.h:46,
from /usr/include/c++/7/string:41,
from /usr/include/c++/7/bits/locale_classes.h:40,
from /usr/include/c++/7/bits/ios_base.h:41,
from /usr/include/c++/7/ios:42,
from /usr/include/c++/7/ostream:38,
from /usr/include/c++/7/iostream:39,
from /home/wcobalt/Files/BoAaP/2/un1/main.cpp:1:
/usr/include/c++/7/ext/new_allocator.h: In instantiation of ‘class __gnu_cxx::new_allocator<B&>’:
/usr/include/c++/7/bits/allocator.h:108:11: required from ‘class std::allocator<B&>’
/usr/include/c++/7/bits/stl_vector.h:81:14: required from ‘struct std::_Vector_base<B&, std::allocator<B&> >::_Vector_impl’
/usr/include/c++/7/bits/stl_vector.h:166:20: required from ‘struct std::_Vector_base<B&, std::allocator<B&> >’
/usr/include/c++/7/bits/stl_vector.h:216:11: required from ‘class std::vector<B&>’
/home/wcobalt/Files/BoAaP/2/un1/main.cpp:7:18: required from here
/usr/include/c++/7/ext/new_allocator.h:63:26: error: forming pointer to reference type ‘B&’
typedef _Tp* pointer;
^~~~~~~
/usr/include/c++/7/ext/new_allocator.h:64:26: error: forming pointer to reference type ‘B&’
typedef const _Tp* const_pointer;
^~~~~~~~~~~~~
In file included from /usr/include/c++/7/string:41:0,
from /usr/include/c++/7/bits/locale_classes.h:40,
from /usr/include/c++/7/bits/ios_base.h:41,
from /usr/include/c++/7/ios:42,
from /usr/include/c++/7/ostream:38,
from /usr/include/c++/7/iostream:39,
from /home/wcobalt/Files/BoAaP/2/un1/main.cpp:1:
/usr/include/c++/7/bits/allocator.h: In instantiation of ‘class std::allocator<B&>’:
/usr/include/c++/7/bits/stl_vector.h:81:14: required from ‘struct std::_Vector_base<B&, std::allocator<B&> >::_Vector_impl’
/usr/include/c++/7/bits/stl_vector.h:166:20: required from ‘struct std::_Vector_base<B&, std::allocator<B&> >’
/usr/include/c++/7/bits/stl_vector.h:216:11: required from ‘class std::vector<B&>’
/home/wcobalt/Files/BoAaP/2/un1/main.cpp:7:18: required from here
/usr/include/c++/7/bits/allocator.h:113:26: error: forming pointer to reference type ‘B&’
typedef _Tp* pointer;
^~~~~~~
/usr/include/c++/7/bits/allocator.h:114:26: error: forming pointer to reference type ‘B&’
typedef const _Tp* const_pointer;
^~~~~~~~~~~~~
In file included from /usr/include/c++/7/vector:64:0,
from /home/wcobalt/Files/BoAaP/2/un1/A.h:5,
from /home/wcobalt/Files/BoAaP/2/un1/main.cpp:2:
/usr/include/c++/7/bits/stl_vector.h: In instantiation of ‘class std::vector<B&>’:
/home/wcobalt/Files/BoAaP/2/un1/main.cpp:7:18: required from here
/usr/include/c++/7/bits/stl_vector.h:247:20: error: no members matching ‘std::vector<B&>::_Base {aka std::_Vector_base<B&, std::allocator<B&> >}::_M_allocate’ in ‘std::vector<B&>::_Base {aka struct std::_Vector_base<B&, std::allocator<B&> >}’
using _Base::_M_allocate;
^~~~~~~~~~~
/usr/include/c++/7/bits/stl_vector.h:248:20: error: no members matching ‘std::vector<B&>::_Base {aka std::_Vector_base<B&, std::allocator<B&> >}::_M_deallocate’ in ‘std::vector<B&>::_Base {aka struct std::_Vector_base<B&, std::allocator<B&> >}’
using _Base::_M_deallocate;
^~~~~~~~~~~~~
...
Вектор ссылок хранить нельзя. Простейшее объяснение - например, указатель может быть перенацелен, т.е. указывать на один объект, а затем на другой, а ссылка - нет. Она инициализируется при объявлении и после этого является точным псевдонимом объекта, по сути неотличимым от самого объекта. Перенацелить ее нельзя, т.е. операции копирования, присвоения - все это будут операции с самим объектом.
Можно в лучшем случае сделать вектор с использованием прокси-класса для ссылок.
Но зачем это вам нужно? Так, как вы планируете работать -
bs.push_back(*(new B()));
даже если бы при этом хранилась ссылка, вам нужно было бы освобождать память как delete &bs[i];
, и постоянно путаться, со значением вы имеете дело или со ссылкой... Подумайте, не удобнее ли для вас работа с vector<unique_ptr<B>>
.
Айфон мало держит заряд, разбираемся с проблемой вместе с AppLab
Перевод документов на английский язык: Важность и ключевые аспекты
Как воспользоваться конструктором копирования родительского класса в конструкторе копирования дочернего?
С клавиатуры вводится строка символов, представляющая собой предложение со знаками препинания (точка, запятая, восклицательный знак)Вывести...