c++ - 将此实例变量添加到 C++11 文件的 header 会使编译器陷入困境。为什么?

标签 c++ c++11 compiler-errors g++ instance-variables

我一直在尝试让我的人工智能程序(用 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; 。现在一切都很完美!那条线有什么问题吗? (在我未简化且更长的原始程序中, hs 是我需要的变量,而不是在这里我将使用它的 hvote 的所有方法配对)
谢谢!

最佳答案

保存非常量引用 vector 时出现问题

std::vector<H&> hs;

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

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

关于c++ - 将此实例变量添加到 C++11 文件的 header 会使编译器陷入困境。为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31169974/

相关文章:

c++ - 用 gcc 编译 std::regex_iterator

c++ - 断言代码不能编译

c++ - 为什么在 C++14 中不推荐使用 std::shuffle 方法?

c++ - 在虚析构函数中调用其他虚方法是否安全?

git - git : imap-send. c:1408 中的编译错误:错误: ‘CURLOPT_USERNAME’ 未声明

python - 考虑到 p 在 python 中是素数,当我执行 p/p+1 + p+1/p 操作时出错

c++ - std::end(myVector) 和 myVector.end() 之间的区别

c++ - 在调用过程中删除 std::function

c++ - 为什么在 C++ 中调用原始类型的构造函数是合法的?

compiler-errors - 简单 C 语法中的移位/减少冲突