删除指向不完整类型“Point”的指针;没有析构函数调用 [英] Deletion of pointer to incomplete type 'Point'; no destructor called

查看:845
本文介绍了删除指向不完整类型“Point”的指针;没有析构函数调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个档案:

Point.h

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

和: Line.h



and: Line.h:

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

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

but when I compile, I got the next error:

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

任何帮助赞赏!

推荐答案

您需要将 #includePoint.h添加到您的文件 Line.h

You need to add #include "Point.h" into your file Line.h. You can only construct and delete complete types.

或者,从 Line.h 中删除成员函数定义。 code>,并将它们放在单独的文件 Line.cpp 中,并包括 Point.h code> 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 file. This is a typical dependency reduction technique which makes code faster to compile, although at a potential loss of certain inlining opportunities.

这篇关于删除指向不完整类型“Point”的指针;没有析构函数调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆