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


i've been trying artificial intelligence program (written in c++11) stop spewing out error messages larger terminal record. i've withered away methods 1 @ time until message went away, , have hence found precise line makes computer go bonkers. here's pertinent part of code:

#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; } 

to me, looks reasonable. however, compiler (g++) disagrees. error messages long terminal doesn't bother saving beginning, don't know true length. however, they're pretty repetitive. last page, instance, reads:

/usr/include/c++/4.8/bits/stl_vector.h: in instantiation of ‘class std::vector<h&>’: hvote.h:16:25:   required 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 reference type ‘h&’    data() _glibcxx_noexcept    ^ /usr/include/c++/4.8/bits/stl_vector.h:886:7: error: forming pointer 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 overloaded    push_back(value_type&& __x)    ^ /usr/include/c++/4.8/bits/stl_vector.h:901:7: error: ‘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 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 ‘std::vector<_tp, _alloc>::~vector() [with _tp = h&; _alloc = std::allocator<h&>]’ hvote.h:10:7:   required 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’ not declared in scope      - this->_m_impl._m_start); }                              ^ 

however, there's 1 little change need make goes away. in hvote.h, delete line, std::vector<h&> hs;. works perfectly! what's problem line? (in not-reduced , longer original program, hs variable need, opposed here i've paired away methods of hvote use it)
thanks!

it has problem keeping vector of non-const references

std::vector<h&> hs; 

i suggest maintaining either vector of values or pointers, depending on if want own these h instances or reference them.

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

Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -