English 中文(简体)
Initialization of reference member requires a temporary variable
原标题:
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

最佳回答

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...
};




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签