【发布时间】:2013-03-28 17:05:31
【问题描述】:
#ifndef SHAPEFACTORY_H_
#define SHAPEFACTORY_H_
#include <istream>
#include <map>
#include <string>
#include "shape.h"
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();
};
#endif
这是标题,我还没有实现任何方法,但我收到以下警告:
Qualifier on function type 'createShapeFunction' (aka 'Shape *()') has unspecified behavior
ps:这个标题是我老师给的,作为作业我必须实现方法
【问题讨论】:
-
你需要在第一组括号内有一个星号:
typedef Shape* (*createShapeFunction)(void); -
就是这样。谢谢!
-
@metal,你能解释一下为什么会这样吗?
-
@Teodora 我已经在我的回答中解释过了。
-
简而言之,第一组括号中的内容是类型的一部分,它需要是一个指针。函数指针的语法总是让我觉得有点奇怪。