删除指向不完整类型'Point'的指针;没有调用析构函数

新手上路,请多包涵

我有 2 个文件:

Point.h :

 class Point {
    int x;
    int y;
    char* name;
   public:
     Point() { name = new char[5]; }
    ~Point() { delete[] name; }
};

和: Line.h

 class Point;
class Line {
    Point* p;
  public:
    Line() {
      p = new Point[2];
      ....
      ...
    }
    ~Line() {
       delete[] p;
    }
};

但是当我编译时,我得到了下一个错误:

 deletion of pointer to incomplete type 'Point'; no destructor called

任何帮助表示赞赏!

原文由 Alon Shmiel 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 646
1 个回答

您需要将 #include "Point.h" 添加到您的文件中 Line.h 。您只能构造和删除 完整 类型。

Alterntively, remove the member function definitions from Line.h , and put them in a separate file Line.cpp , and include Point.h and Line.h in that 文件。这是一种典型的依赖减少技术,可以使代码更快地编译,尽管可能会失去某些内联机会。

原文由 Kerrek SB 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题