I have a class in whose constructor I want to pass parameters in the form of enum .

 class PressureValue { public: enum pressureMeasureUnit {puUnknown, puAuto, puAtmosphere, puTechAtmosphere, puKgCm2, puPascal, puMillimetreOfMercury, puTor, puMillibar}pressureMeasureUnitP; PressureValue(PressureValue::pressureMeasureUnit defaultUnit = puKgCm2){ defaultPressureUnit = defaultUnit; } ~PressureValue(){} float value = 0.0; //перегрузка оператора PressureValue& operator=(float f){ value = f; return *this; } operator float() const{ return value; } private: pressureMeasureUnit defaultPressureUnit; pressureMeasureUnit convertingUnit; }; class PressureDrop { private: PressureValue declinePressure, declinePressureUp, declinePressureDown, declinePressureDelta; //PressureValue environmentPressure; PressureValue environmentPressure(PressureValue::puMillimetreOfMercury); int declinePressureTime, environmentTemperature; public: PressureDrop(){} ~PressureDrop(){} }; int main(){ PressureValue p(PressureValue::puMillibar); } 

If this is really done in main , then everything is fine. And if I want to create an instance of the class PressureValue as a private field of some class, then I get the error:

error: 'PressureValue :: puMillimetreOfMercury' is not a type

How can you get out of this situation?

  • @VTT fixed the code. And I wrote that in main everything is fine. and in the fields of the class is not very - Marat Gareev
  • @VTT thanks, removed. In the original, it is inherited from another class. I did not notice when I simplified the code - Marat Gareev

1 answer 1

As I understand it, you are trying to initialize the environmentPressure field during the declaration (and not to declare a function). To do this, use list-initialization :

 PressureValue environmentPressure{PressureValue::puMillimetreOfMercury}; 

With the transition to C ++ 11, list initialization should always be used.

  • You all understood correctly. Live and learn, for the first time I see this. Thank you very much for the explanation! - Marat Gareev