将这个实例变量添加到C++11文件的头中会使编译器陷入困境.为什么?
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;
。现在一切都很完美!那条线有什么问题?(在我的非精简且更长的原始程序中,hs
是我需要的变量,而在这里我已经将使用它的hvote
的所有方法配对)
谢谢
它在保存一个非常量引用向量时遇到问题
std::vector<H&> hs;
我建议维护值或指针的向量,这取决于您是想拥有这些H
实例还是只引用它们。
std::vector<H> hs;
std::vector<H*> hs;
相关文章:
- 为什么所有C++编译器都会崩溃或挂起此代码
- 为什么C++编译器没有检测到正确声明的类?
- 为什么布尔开关语句有编译器警告?
- 为什么编译器将其解析为函数指针而不是递归调用?
- C++为什么我的编译器成功了,但我的计算机给出了调试错误?
- C++初学者:为什么我的编译器根据我的循环返回"name not found"?
- 为什么我的编译器无法弄清楚这种转换,它何时存在?
- 为什么编译器说"candidate template ignored: couldn't infer template argument 'InputIterator'"?
- 为什么编译器不检查被覆盖函数的存储类?
- 为什么编译器在使用"无常量复制构造函数"时抱怨?
- 为什么 std::count 比 MSVC 编译器的普通循环慢,但与 GCC 相等?
- 为什么 gcc 编译器标志未知?
- 当类型适当的构造函数可用时,为什么一个编译器尝试使用已删除的副本构造函数
- C 17 GCC编译器为什么会发出有关未定义的警告
- Clang 编译器为什么无法编译以下variadic模板代码
- 编译器为什么抱怨对齐
- 编译器为什么要将浮点数字的位数固定为6
- 编译器为什么要生成此程序集
- vtkCharts库既不适用于Visual Studio,也不适用于GCC编译器 - 为什么?
- 在c++中,当常量也可以工作时,编译器为什么选择非常量函数