struct Div
{
int i;
int j;
};
class A
{
public:
A();
Div& divs;
};
In my constructor definition, I have the following implementation
A::A(): divs(NULL)
{}
I get the following error:
Error72 error C2354: 'A::divs' : initialization of reference member requires a temporary variable
There are three steps to initializing a reference variable from scratch: declaring the reference variable; using the new operator to build an object and create a reference to the object; and. storing the reference in the variable.
An rvalue reference can be initialized with an lvalue in the following contexts: A function lvalue. A temporary converted from an lvalue. An rvalue result of a conversion function for an lvalue object that is of a class type.
To initialize the const value using constructor, we have to use the initialize list. This initializer list is used to initialize the data member of a class. The list of members, that will be initialized, will be present after the constructor after colon. members will be separated using comma.
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) {}
If you want it to be able to be "null", then you need a pointer, not a reference.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With