我是 C++ 中这种工厂模式的新手,但在尝试实现其中一种方法时,我得到了标题,但出现以下错误:
静态成员函数中成员“creationFunctions”的无效使用
typedef Shape (*createShapeFunction)(void);
/* thrown when a shape cannot be read from a stream */
class WrongFormatException { };
class ShapeFactory {
public:
static void registerFunction(const std::string &string, const createShapeFunction *shapeFunction);
static Shape *createShape(const std::string &string);
static Shape *createShape(std::istream &ins);
private:
std::map<std::string, createShapeFunction *> creationFunctions;
ShapeFactory();
static ShapeFactory *getShapeFactory();
};
void ShapeFactory::registerFunction(const std::string &string, const createShapeFunction *shapeFunction)
{
creationFunctions.at(string) = shapeFunction;
}