当文件拆分为 .cpp 和 .h 文件时,从派生类指针到基类指针的转换不起作用

发布于 2024-12-10 23:46:24 字数 1110 浏览 0 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不爱素颜 2024-12-17 23:46:24

您需要从 Shape.h 中删除 #include "Square.h"#include "Circle.h"

SquareCircle 依赖于 Shape 的定义,因为它们都继承自 ShapeShape code> 可以而且必须在定义 SquareCircle 之前定义,因此包括 CircleSquare 的定义在定义 Shape 之前不正确。

You need to remove #include "Square.h" and #include "Circle.h" from Shape.h.

Square and Circle depend on a definition of Shape because they both inherit from Shape but Shape can - and must - be defined before Square and Circle are defined so including definitions of Circle and Square before you define Shape is incorrect.

梦回旧景 2024-12-17 23:46:24

您创建了一个循环包含。 Shape.h 包括Circle.hSquare.hCircle.hSquare.h 包括 Shape.h。这没有任何意义。

显然,您还使用了某种类型的包含防护,您在这里没有向我们展示这些防护(如果您不这样做,错误将会有所不同)。包含防护确保 Shape.h 不包含在其自身中。结果是 Square.hShape 定义之前被包含到 Shape.h 中,这就是您收到错误的原因。

循环包含总是没有用的。为什么

#include "Circle.h"
#include "Square.h"

出现在 Shape.h 中?这些包括在那里做什么?您必须确保不将 Circle.hSquare.h 包含到 Shape.h 中。

You created a circular inclusion. Shape.h is including Circle.h and Square.h. Circle.h and Square.h include Shape.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 that Square.h is included into Shape.h before the definition of Shape, which is why you get the error.

Circular inclusion is always useless. Why is

#include "Circle.h"
#include "Square.h"

present in Shape.h? What are these includes doing there? You have to make sure that you do not include Circle.h and Square.h into Shape.h.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文