【发布时间】:2012-07-25 09:56:53
【问题描述】:
我需要在类定义中创建一个静态对象。在 Java 中是可能的,但在 C++ 中我得到一个错误:
../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
我的班级是这样的:
#include <string>
class PlaceID {
public:
inline PlaceID(const std::string placeName):mPlaceName(placeName) {}
const static PlaceID OUTSIDE = PlaceID("");
private:
std::string mPlaceName;
};
是否可以在这个类中创建一个类的对象?它必须具备哪些先决条件?
【问题讨论】:
标签: c++ static-members