0

I have a class with a member variable of another class:

class MeasurementUnit {
private:
    MeasurementMultiplier _multiplier;

Actually I would not need a default constructor for MeasurementMultiplier, because actually I will initialize with parameters MeasurementMultiplier(a,b,c), and I would - but can't directly:

C2864: 'MeasurementUnit::_multiplier' :
only static const integral data members can be initialized within a class

So I need the default constructor, without it does not compile error: C2512: 'MeasurementUnit' : no appropriate default constructor available

Can I avoid needing the default constructor?

Horst Walter
  • 13,663
  • 32
  • 126
  • 228

2 Answers2

6

In all constructors of your class MeasurementUnit, you need to initialize the member variable _multiplier in the initializer list. Example:

MeasurementUnit::MeasurementUnit()
  : _multiplier(1,2,3)
{}
Daniel Frey
  • 55,810
  • 13
  • 122
  • 180
  • Sorry,I do not get it. By doing so you have a default constructor,this is what I want to avoid. – Horst Walter Mar 14 '13 at 21:45
  • 1
    @HorstWalter: In this case I don't get your question. The *example* I gave refers to the (default) constructor of `MeasurementUnit` to avoid the need for a default constructor of `MeasurementMultiplier`. If this is not what you wanted, please edit and clarify the question. – Daniel Frey Mar 14 '13 at 21:49
  • 1
    @HorstWalter: And I just realized *my* mistake of mixing up the member variable's name and its type. I edited the answer accordingly, sorry for the confusion it might have caused. – Daniel Frey Mar 15 '13 at 09:58
1

Use MIL - Member Initialization List MIL

Community
  • 1
  • 1
user1708860
  • 1,683
  • 13
  • 32