【发布时间】:2013-03-21 10:01:42
【问题描述】:
我正在尝试在我的 c++ 程序中包含文件,但我一直遇到错误:
ShapeVisitor.h:9:28: error: ‘Circle’ has not been declared
我认为问题在于类的结构方式会导致循环依赖。我该如何解决?
类标题如下...
//Circle.h
#ifndef CIRCLE_H
#define CIRCLE_H
// headers, ...
#include "Shape.h"
class Circle: public Shape {
//class declaration
}
#endif
//Shape.h
#ifndef SHAPE_H
#define SHAPE_H
// headers, ...
#include <iostream>
class Shape {
//a certain method in the class declaration looks like this
virtual void accept(ShapeVisitor& v) = 0;
//rest of class
}
#endif
//ShapeVisitor.h
#ifndef SHAPEVISITOR_H
#define SHAPEVISITOR_H
#include "Circle.h"
class ShapeVisitor {
//a method in the class looks like this:
virtual void visitCircle(Circle *s) = 0;
//rest of class
}
#endif
如您所见,圆形包括形状,其中包括 shapevisitor,同样,形状也包括圆形。
有什么想法吗?
【问题讨论】:
-
如你所见...我一定是瞎子,我没看到。我看到的是没有包含,所以 Cyrcle 是未声明的。
-
你的类定义需要分号。
-
也许this 可以帮助您详细了解发生了什么
标签: c++ header include forward-declaration