Initialization of reference member requires a temporary variable C++

后端 未结 12 1905
自闭症患者
自闭症患者 2020-12-10 03:29
struct Div
{
   int i;
   int j;
};   

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

In my constructor definition, I have

12条回答
  •  渐次进展
    2020-12-10 04:16

    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 )
    {}
    

提交回复
热议问题