当文件拆分为 .cpp 和 .h 文件时,从派生类指针到基类指针的转换不起作用
Shape.h
#include"Circle.h"
#include"Square.h"
class Shape {
public:
virtual void Draw() = 0;
static Shape* Create(std::string type);
};
Shape.cpp
#include "Shape.h"
Shape* Shape::Create(string type) {
if ( type == "circle" ) return new Circle();
if ( type == "square" ) return new Square();
return NULL;
}
Circle.cpp
#include "Circle.h"
void Circle::Draw() {
cout << "I am circle" << endl;
}
Circle.h
#include"Shape.h"
class Circle:public Shape {
public:
void Draw();
friend class Shape;
};
Square.cpp
#include "Square.h"
void Square::Draw() {
cout << "I am Square" << endl;
}
Square.h
#include"Shape.h"
class Square:public Shape {
public:
void Draw();
friend class Shape;
};
它抛出此错误: Square.h 错误:预期的类名之前 { token [对于继承,它不识别 Shape]
但如果它位于单个 main.cpp 文件中的整体文件(没有 .cpp 和 .h)中,则相同的代码可以工作 我在包含头文件时缺少什么?
提前致谢。
Shape.h
#include"Circle.h"
#include"Square.h"
class Shape {
public:
virtual void Draw() = 0;
static Shape* Create(std::string type);
};
Shape.cpp
#include "Shape.h"
Shape* Shape::Create(string type) {
if ( type == "circle" ) return new Circle();
if ( type == "square" ) return new Square();
return NULL;
}
Circle.cpp
#include "Circle.h"
void Circle::Draw() {
cout << "I am circle" << endl;
}
Circle.h
#include"Shape.h"
class Circle:public Shape {
public:
void Draw();
friend class Shape;
};
Square.cpp
#include "Square.h"
void Square::Draw() {
cout << "I am Square" << endl;
}
Square.h
#include"Shape.h"
class Square:public Shape {
public:
void Draw();
friend class Shape;
};
It throws this error:
Square.h error: expected class-name before { token [for the inheritance it does not identify Shape]
But the same code works if it is in a monolithic file(without the .cpp and .h) a single main.cpp file
What is that I am missing with including header files?
Thanks in Advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要从
Shape.h
中删除#include "Square.h"
和#include "Circle.h"
。Square
和Circle
依赖于Shape
的定义,因为它们都继承自Shape
但Shape
code> 可以而且必须在定义Square
和Circle
之前定义,因此包括Circle
和Square
的定义在定义Shape
之前不正确。You need to remove
#include "Square.h"
and#include "Circle.h"
fromShape.h
.Square
andCircle
depend on a definition ofShape
because they both inherit fromShape
butShape
can - and must - be defined beforeSquare
andCircle
are defined so including definitions ofCircle
andSquare
before you defineShape
is incorrect.您创建了一个循环包含。
Shape.h
包括Circle.h
和Square.h
。Circle.h
和Square.h
包括Shape.h
。这没有任何意义。显然,您还使用了某种类型的包含防护,您在这里没有向我们展示这些防护(如果您不这样做,错误将会有所不同)。包含防护确保
Shape.h
不包含在其自身中。结果是Square.h
在Shape
定义之前被包含到Shape.h
中,这就是您收到错误的原因。循环包含总是没有用的。为什么
出现在
Shape.h
中?这些包括在那里做什么?您必须确保不将Circle.h
和Square.h
包含到Shape.h
中。You created a circular inclusion.
Shape.h
is includingCircle.h
andSquare.h
.Circle.h
andSquare.h
includeShape.h
. This does not make any sense.Apparently you are also using include guards of some sort, which you are not showing to us here (if you didn't the error would be different). The include guards make sure that
Shape.h
is not included into itself. The result is thatSquare.h
is included intoShape.h
before the definition ofShape
, which is why you get the error.Circular inclusion is always useless. Why is
present in
Shape.h
? What are these includes doing there? You have to make sure that you do not includeCircle.h
andSquare.h
intoShape.h
.