Define a variable of a static object const inside a class

I need to create a static object inside a class definition. This is possible in Java, but in C ++ I get an error:

../PlaceID.h:9:43: error: invalid use of incomplete type 'class PlaceID' ../PlaceID.h:3:7: error: forward declaration of 'class PlaceID' ../PlaceID.h:9:43: error: invalid in-class initialization of static data 

My class is as follows:

 #include <string> class PlaceID { public: inline PlaceID(const std::string placeName):mPlaceName(placeName) {} const static PlaceID OUTSIDE = PlaceID(""); private: std::string mPlaceName; }; 

Is it possible to make a class object inside this class? What are the prerequisites that he must observe?

+4
source share
1 answer

You cannot define a member variable because the class is not yet fully defined. You should do it as follows:

 class PlaceID { public: inline PlaceID(const std::string placeName):mPlaceName(placeName) {} const static PlaceID OUTSIDE; private: std::string mPlaceName; }; const PlaceID PlaceID::OUTSIDE = PlaceID(""); 
+11
source

All Articles