删除指向不完整类型“Point”的指针;没有析构函数调用 [英] Deletion of pointer to incomplete type 'Point'; no destructor called
问题描述
我有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屋!