将此实例变量添加到 C++11 文件的 header 会使编译器崩溃。为什么?

Adding this instance variable to the header of a C++11 file drives the compiler up the wall. Why?

我一直在努力让我的人工智能程序(用 C++11 编写)停止输出比我的终端记录的大的错误消息。我一次一个地减少方法,直到消息消失,因此找到了使计算机变得疯狂的精确线路。这是我的代码的相关部分:

#include <iostream>
#include <vector>
#include <algorithm>
#include <math.h>

class H
{
    public:
        H(int);
        int get_val();
    private:
        int val;
};

class Hvote
{
    public:
        Hvote() : error(1.0) { }
        std::vector<H&> hs;
        double error;
};

int main()
{
    Hvote hvote;
    return 0;
}

对我来说,这一切看起来都很合理。但是,编译器 (g++) 不同意。错误消息太长了,我的终端甚至懒得保存开头,所以我不知道它们的真实长度。但是,它们非常重复。例如,最后一页是:

/usr/include/c++/4.8/bits/stl_vector.h: In instantiation of ‘class std::vector<H&>’:
hvote.h:16:25:   required from here
/usr/include/c++/4.8/bits/stl_vector.h:237:20: error: no members matching ‘std::vector<H&>::_Base {aka std::_Vector_base<H&, std::allocator<H&> >}::_M_allocate’ in ‘std::vector<H&>::_Base {aka struct std::_Vector_base<H&, std::allocator<H&> >}’
   using _Base::_M_allocate;
                ^
/usr/include/c++/4.8/bits/stl_vector.h:238:20: error: no members matching ‘std::vector<H&>::_Base {aka std::_Vector_base<H&, std::allocator<H&> >}::_M_deallocate’ in ‘std::vector<H&>::_Base {aka struct std::_Vector_base<H&, std::allocator<H&> >}’
   using _Base::_M_deallocate;
                ^
/usr/include/c++/4.8/bits/stl_vector.h:878:7: error: forming pointer to reference type ‘H&’
   data() _GLIBCXX_NOEXCEPT
   ^
/usr/include/c++/4.8/bits/stl_vector.h:886:7: error: forming pointer to reference type ‘H&’
   data() const _GLIBCXX_NOEXCEPT
   ^
/usr/include/c++/4.8/bits/stl_vector.h:919:7: error: ‘void std::vector<_Tp, _Alloc>::push_back(std::vector<_Tp, _Alloc>::value_type&&) [with _Tp = H&; _Alloc = std::allocator<H&>; std::vector<_Tp, _Alloc>::value_type = H&]’ cannot be overloaded
   push_back(value_type&& __x)
   ^
/usr/include/c++/4.8/bits/stl_vector.h:901:7: error: with ‘void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = H&; _Alloc = std::allocator<H&>; std::vector<_Tp, _Alloc>::value_type = H&]’
   push_back(const value_type& __x)
   ^
/usr/include/c++/4.8/bits/stl_vector.h: In instantiation of ‘std::vector<_Tp, _Alloc>::~vector() [with _Tp = H&; _Alloc = std::allocator<H&>]’:
hvote.h:10:7:   required from here
/usr/include/c++/4.8/bits/stl_vector.h:416:30: error: ‘struct std::_Vector_base<H&, std::allocator<H&> >::_Vector_impl’ has no member named ‘_M_start’
     _M_get_Tp_allocator()); }
                          ^
/usr/include/c++/4.8/bits/stl_vector.h:416:30: error: ‘struct std::_Vector_base<H&, std::allocator<H&> >::_Vector_impl’ has no member named ‘_M_finish’
/usr/include/c++/4.8/bits/stl_vector.h: In instantiation of ‘std::_Vector_base<_Tp, _Alloc>::~_Vector_base() [with _Tp = H&; _Alloc = std::allocator<H&>]’:
/usr/include/c++/4.8/bits/stl_vector.h:416:33:   required from ‘std::vector<_Tp, _Alloc>::~vector() [with _Tp = H&; _Alloc = std::allocator<H&>]’
hvote.h:10:7:   required from here
/usr/include/c++/4.8/bits/stl_vector.h:161:33: error: ‘struct std::_Vector_base<H&, std::allocator<H&> >::_Vector_impl’ has no member named ‘_M_start’
     - this->_M_impl._M_start); }
                             ^
/usr/include/c++/4.8/bits/stl_vector.h:161:9: error: ‘struct std::_Vector_base<H&, std::allocator<H&> >::_Vector_impl’ has no member named ‘_M_start’
     - this->_M_impl._M_start); }
     ^
/usr/include/c++/4.8/bits/stl_vector.h:161:9: error: ‘struct std::_Vector_base<H&, std::allocator<H&> >::_Vector_impl’ has no member named ‘_M_end_of_storage’
/usr/include/c++/4.8/bits/stl_vector.h:161:33: error: ‘_M_deallocate’ was not declared in this scope
     - this->_M_impl._M_start); }
                             ^

但是,我只需要做一点点改变就可以让这一切消失。在 hvote.h 中,只需删除行 std::vector<H&> hs;。现在一切正常!那条线有什么问题? (在我的 not-reduced 和更长的原始程序中,hs 是我需要的一个变量,而不是在这里我已经配对使用它的 hvote 的所有方法)
谢谢!

您保留非常量引用向量时出现问题

std::vector<H&> hs;

我建议维护一个值向量或指针,具体取决于您是想拥有这些 H 实例还是仅引用它们。

std::vector<H> hs;
std::vector<H*> hs;