代码之家  ›  专栏  ›  技术社区  ›  Alon Shmiel

删除指向不完整类型“Point”的指针;未调用析构函数

  •  13
  • Alon Shmiel  · 技术社区  · 11 年前

    我有两个文件:

    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
    

    感谢您的帮助!

    4 回复  |  直到 11 年前
        1
  •  18
  •   Kerrek SB    11 年前

    您需要添加 #include "Point.h" 进入您的文件 Line.h 。只能构造和删除 完成 类型。

    或者,从中删除成员函数定义 第h行 ,并将它们放在一个单独的文件中 Line.cpp ,包括 Point.h 第h行 在里面 那个 文件这是一种典型的依赖性减少技术,它可以使代码更快地编译,尽管可能会失去某些内联机会。

        2
  •  5
  •   Ed Swangren    11 年前

    您已向前申报 Point ,这对于声明指针或引用很好,但对于编译器需要知道前向声明类的定义的其他任何事情都不好。

    如果您需要头文件中的正向声明(是吗?如果不是,只需要 #include "Point.h" 在里面 Line.h )然后执行您的 Line 实现文件中的函数 #include s Point.h .

        3
  •  0
  •   Jan Kundrát    11 年前

    对其他人给出的建议进行一点扩展——一条线总是由两个端点定义的。将这些点定义为堆分配的内存没有多大意义。为什么不让这两点成为 Line 班这将节省内存,提高性能,并产生更干净的代码。你必须包括 "Point.h" 不过,要让它发挥作用。

        4
  •  0
  •   Dmitry Ivanov    2 年前

    带有命名空间的正向声明示例。

    // my header.h
    namespace Poco {
        class TextConverter;
    }