将此实例变量添加到C++ 11文件的标题中可以使编译器更上一层楼.为什么?

Qua*_*ool 2 c++ compiler-errors g++ instance-variables c++11

我一直试图让我的人工智能程序(用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;
}
Run Code Online (Sandbox Code Playgroud)

对我来说,一切看起来都很合理.但是,编译器(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); }
                             ^
Run Code Online (Sandbox Code Playgroud)

然而,我需要做的只有一点点变化,这一切都会消失.在hvote.h,只需删除该行,std::vector<H&> hs;.现在一切都很完美!这条线有什么问题?(在我没有减少和更长的原始程序中,hs是一个我需要的变量,而不是在这里,我已经配对了hvote使用它的所有方法)
谢谢!

Cor*_*mer 5

保持非const引用的向量存在问题

std::vector<H&> hs;
Run Code Online (Sandbox Code Playgroud)

我建议维护一个值向量或指针,这取决于你是想拥有这些H实例还是只引用它们.

std::vector<H> hs;
std::vector<H*> hs;
Run Code Online (Sandbox Code Playgroud)

  • 或者`std :: reference_wrapper <H>`如果你想要的东西比指针更"引用". (2认同)