Initialization of reference member requires a temporary variable C++(引用成员的初始化需要一个临时变量 C++)
问题描述
struct Div
{
int i;
int j;
};
class A
{
public:
A();
Div& divs;
};
在我的构造函数定义中,我有以下内容
In my constructor definition, I have the following
A::A() : divs(NULL)
{}
我收到以下错误:
Error72 error C2354:
'A::divs' : initialization of reference member requires a temporary variable
推荐答案
必须初始化引用才能引用某事;它不能引用任何内容,因此您不能默认构造一个包含一个类的类(除非像其他人建议的那样,您定义了一个全局空"值).您将需要一个带有 Div
的构造函数来引用:
A reference must be initialised to refer to something; it can't refer to nothing, so you can't default-construct a class that contains one (unless, as others suggest, you define a global "null" value). You will need a constructor that is given the Div
to refer to:
explicit A(Div &d) : divs(d) {}
如果您希望它能够为null",那么您需要一个指针,而不是一个引用.
If you want it to be able to be "null", then you need a pointer, not a reference.
这篇关于引用成员的初始化需要一个临时变量 C++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:引用成员的初始化需要一个临时变量 C++


- 与 int by int 相比,为什么执行 float by float 矩阵乘法更快? 2021-01-01
- C++ 协变模板 2021-01-01
- 静态初始化顺序失败 2022-01-01
- 一起使用 MPI 和 OpenCV 时出现分段错误 2022-01-01
- 使用/clr 时出现 LNK2022 错误 2022-01-01
- 近似搜索的工作原理 2021-01-01
- Stroustrup 的 Simple_window.h 2022-01-01
- 从python回调到c++的选项 2022-11-16
- STL 中有 dereference_iterator 吗? 2022-01-01
- 如何对自定义类的向量使用std::find()? 2022-11-07