RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 948618
Accepted
wcobalt
wcobalt
Asked:2020-02-24 05:58:55 +0000 UTC2020-02-24 05:58:55 +0000 UTC 2020-02-24 05:58:55 +0000 UTC

访问链接向量的元素

  • 772

假设我们有一个类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;
                   ^~~~~~~~~~~~~

...

c++
  • 1 1 个回答
  • 10 Views

1 个回答

  • Voted
  1. Best Answer
    Harry
    2020-02-24T13:56:24Z2020-02-24T13:56:24Z

    无法存储参考向量。最简单的解释——例如,指针可以被重定向,即 指向一个对象,然后指向另一个对象,而引用则没有。它在声明时被初始化,此后是对象的确切别名,与对象本身本质上没有区别。它不能被重定向;复制、赋值操作——所有这些都是对对象本身的操作。

    您最多可以使用链接的代理类制作矢量。

    但是你为什么需要它?你打算怎么工作——

    bs.push_back(*(new B()));
    

    даже если бы при этом хранилась ссылка, вам нужно было бы освобождать память как delete &bs[i];, и постоянно путаться, со значением вы имеете дело или со ссылкой... Подумайте, не удобнее ли для вас работа с vector<unique_ptr<B>>.

    • 7

相关问题

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    根据浏览器窗口的大小调整背景图案的大小

    • 2 个回答
  • Marko Smith

    理解for循环的执行逻辑

    • 1 个回答
  • Marko Smith

    复制动态数组时出错(C++)

    • 1 个回答
  • Marko Smith

    Or and If,elif,else 构造[重复]

    • 1 个回答
  • Marko Smith

    如何构建支持 x64 的 APK

    • 1 个回答
  • Marko Smith

    如何使按钮的输入宽度?

    • 2 个回答
  • Marko Smith

    如何显示对象变量的名称?

    • 3 个回答
  • Marko Smith

    如何循环一个函数?

    • 1 个回答
  • Marko Smith

    LOWORD 宏有什么作用?

    • 2 个回答
  • Marko Smith

    从字符串的开头删除直到并包括一个字符

    • 2 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5