struct Div
{
int i;
int j;
};
class A
{
public:
A();
Div& divs;
};
在我的构造函数定义中,我有以下实现
A::A(): divs(NULL)
{}
我收到以下错误:
Error72 error C2354: 'A::divs' : initialization of reference member requires a temporary variable
最佳答案
必须初始化引用以引用某物;它不能引用任何内容,因此您不能默认构造一个包含一个类的类(除非像其他人建议的那样,您定义了一个全局“null”值)。您将需要一个被赋予 Div
的构造函数来引用:
explicit A(Div &d) : divs(d) {}
如果你希望它能够为“null”,那么你需要一个指针,而不是一个引用。
关于c++ - 引用成员的初始化需要一个临时变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1701416/