0

I have one ABC class whose constructor is taking 3 arguments e.g x1, x2 and l. One sample code is shown below. I am trying to make another constructor in the same class ABC that should take different arguments but, I am unable to do. It might be a very general question but I am unable to get satisfactory answers.

     class ABC {
      protected:
      X1& _x1;
      X2& _x2;
      Logger& _logger;
      ABC(X1& x1, X2& x2, Logger& l):_x1(x1), _x2(x2),_logger(l) {}
      ABC(X1& x1, Logger& l):_x1(x1),_logger(l) {} //getting error uninitialized reference member ‘ABC::_x2’
      ~ABC(){this->clear();}
      void clear(){}
      };

error uninitialized reference member ‘ABC::_x2'

1
  • You are missing an initializer of reference _x2. If the reference to X2 is optional maybe you should use a pointer to the X2 object or set _x2 to a static instance of X2. Commented May 25, 2015 at 20:18

4 Answers 4

1

The compiler is telling you the truth - you need to initialize the second reference in your class, i.e. _x2.

You cannot have uninitialized references so either don't use them or you need to initialize them in every constructor that you declare.

Sign up to request clarification or add additional context in comments.

2 Comments

Okay, does it means that there is no way to make 2nd constructor for the above example ?
If you insist on _x2 being a reference then you need to initialise it in the second constructor. Otherwise don't declare it as a reference. Look at dasblinkenlight's answer.
1

Well, thecompiler tells you what the problem is (and it is inrelated to having multiple constructors): your class has a reference member which is not initialized in the constructor: _x2. All reference members need to be initialized in the constructor.

Comments

1

C++ requires you to initialize all reference members. If there are situations when a reference to an object is optional, it's best to replace a reference variable with a pointer:

X2* _x2;

If you must have a reference with a default, make a private static variable for it, and use that variable to initialize your reference variable:

class ABC {
private:
    static X2 _x2_default; // Declare it in a CPP file
protected:
    ...
    ABC(X1& x1, Logger& l):_x1(x1),_logger(l), _x2(_x2_default) {}
    ...
};

2 Comments

I think this is the answer I am searching for. Thank you.
@musafir You are welcome! Try this out and see if the approach works for you. If it does, please consider accepting the answer by clicking the grey check mark next to it. This would let others know that your problem is solved, and earn you a new badge on Stack Overflow.
0

A reference must be initialised. For class members, this means initialisation in the member initializer list of the constructor.

If the member may be remain uninitialized, make it a pointer and ensure that it is default initialized (ie :m2_() //null, where m2_ is M2* m2_;), or wrap it with a smart pointer that at least ensures default initialization

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.