mercredi 1 juillet 2015

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

I've been trying to get my artificial intelligence program (written in C++11) to stop spewing out error messages larger than my Terminal will record. I've withered away methods one at a time until the message went away, and have hence found the precise line which makes the computer go bonkers. Here's the pertinent part of my code:

//h.h


#ifndef __H_H_INCLUDED__
#define __H_H_INCLUDED__

#include <iostream>
#include <vector>

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

#endif


//hvote.h


#ifndef __HVOTE_H_INCLUDED__
#define __HVOTE_H_INCLUDED__

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

class Hvote
{
    public:
        Hvote();
        //This method is the class's constructor.
        //It sets error to 1.0.
        std::vector<H&> hs;
        double error;
};

#endif


//hvote.cpp


#include "hvote.h"

Hvote::Hvote()
{
    error = 1.0;
}


//main.cpp


#include <iostream>
#include <vector>
#include "h.h"
#include "hvote.h"

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

To me, it all looks reasonable. However, the compiler (g++) disagrees. The error messages are so long that my Terminal doesn't even bother saving the beginning, so I don't know their true length. However, they're pretty repetitive. The last page, for instance, reads:

/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); }
                             ^

However, there's only one little change I need to make so that this all goes away. In hvote.h, just delete the line, std::vector<H&> hs;. Now everything works perfectly! What's the problem with that line? (In my not-reduced and much longer original program, hs is a variable I need, as opposed to here where I've paired away all the methods of hvote that use it)
Thanks!

Aucun commentaire:

Enregistrer un commentaire