简体   繁体   中英

Initialization of reference member requires a temporary variable 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)
{}

I get the following error:

  Error72 error C2354: 
  'A::divs' : initialization of reference member requires a temporary variable 

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.

divs is a reference, not a pointer. You can't set it to NULL, it has to point to an actual object of some kind. The best thing to do here is probably to define a static/global instance of Div that you arbitrarily define to be the "Null Div" (set its value to something you're unlikely ever to use) and initialize div to that. Something like this:

struct Div
{
   int i;
   int j;
};   

Div NULL_DIV = { INT_MAX, INT_MAX };

class A
{
    public:
             A();
             Div& divs;
};


A::A() : divs(NULL_DIVS)
{
}

Or, alternatively, just make divs a pointer instead of a reference.

*Note that you can't use a const reference unless you cast away the constness because by default the compiler won't allow you to assign a cosnt ref to a non-const ref.

For one, you can't have a NULL reference. As second, all variable references in a class must be initialized at construction time.

In "English": a reference refers to something . It cannot refer to nothing (null). That's why references are safer to use then pointers.

References have to reference something. There is no such thing as a null reference in the C++ language. If the member may not have a value, then it should be a pointer, or a boost::optional or some type like that.

A reference must be initialized to reference a valid object.

Keep in mind that once a reference is initialized to point to something, you cannot alter it to point to something else. If this is not the desired behavior, then you should use a pointer or a copy of the object instead.

The compiler message is one of those messages that don't make sense from the language point of view, but reveal the inner workings of the compiler, the sequence in which its inner logic works.

In your case you are using an rvalue ( NULL ) to initialize a reference. When such initialization is allowed, the rvalue is converted to a temporary object, to which the reference will be bound. So, the compiler has realized it right away and informed you about the fact with a diagnostic message.

In reality though, the trick like that is only allowed for const references, so your code is broken, since the reference is not const in our case. Also, a struct reference, as the one in your code, cannot be initialized with NULL rvalue (which has integral type), so it is broken for that reason as well.

The compiler's message is rather misleading though. The text of the message seems to imply that it is illegal to initialize member references with temporary objects. In fact, this is legal in C++ (once the above problems are fixed), although it makes no sense. But, I guess, once ill-formed code is accompanied by at least some error message, it should be OK for everyone...

Plain and simple:

A reference can never be NULL.

You should try and initialise your "divs" variable. You cannot have a reference refering to "nothing"...

Have a look here :

http://msdn.microsoft.com/en-us/library/bbt3ewya%28VS.80%29.aspx

Hope this help a bit!

As noted in other posts, references (Div&) cannot be null. So the most straightforward change you can make is to provide a reference in the constructor and initialize your reference member. Like this,

class A
{
    public:
             A(Div& inDivs);
             Div& divs;

};

public A::A( Div& inDivs )
: divs( inDivs )
{}
class A
{
    Div & ref(Div div) { return div; }
    public:
       A() : divs(ref(Div())) {};
       Div& divs;
};

I would suggest:

class A{
    std::deque<object*>& que = *(std::deque<object*> *)0;
    // ...
    // Of course `que` is to  be assigned valid deque before using it - just like pointers...
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM