删除指向不完整类型"点"的指针;未调用析构函数
Deletion of pointer to incomplete type 'Point'; no destructor called
我有两个文件:
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
感谢任何帮助!
您需要将#include "Point.h"
添加到文件Line.h
中。只能构造和删除完整的类型。
或者,从Line.h
中删除成员函数定义,并将它们放在单独的文件Line.cpp
中,并在文件中包含Point.h
和Line.h
。这是一种典型的依赖性减少技术,它使代码的编译速度更快,尽管可能会失去某些内联机会。
您有前向声明的Point
,这对于声明指针或引用很好,但对于编译器需要知道前向声明类的定义的其他任何事情都不好。
如果你需要头文件中的forward声明(你需要吗?)如果没有,就在Line.h
中的#include "Point.h"
),然后在#include
中的Point.h
的实现文件中实现Line
函数。
扩展一下别人的建议——一条线总是由两个端点定义的。将这些点定义为堆分配内存没有多大意义。为什么不让这两点成为Line
类的常规成员呢?这将节省内存,提高性能,并导致一个更干净的代码。但是,您必须包含"Point.h"
才能正常工作。
使用名称空间向前声明示例。
// my header.h
namespace Poco {
class TextConverter;
}
相关文章:
- 优先顺序:智能指针和类析构函数
- 使用基类指针创建对象时,缺少派生类析构函数
- 是否可以使用函数指针调用虚拟析构函数?
- 当指针在 cpp 17 中失去引用时,是否会调用非默认析构函数?
- 如何从类成员函数返回指针,例如 size_t * class :: function(); 并使用类析构函数 ~size
- 如何将析构函数分配给指针
- 智能指针析构函数争用条件
- 是否可以在其析构函数中使用指向已销毁对象的指针?
- 在C++中删除指针数组时析构函数崩溃
- C++ 由于类析构函数中的指针设置为 NULL 而导致的内存泄漏
- 如果引用应该保留,不删除析构函数中的指针会导致内存泄漏吗?
- 删除析构函数C++中的指针
- 构造函数是否unique_ptr初始化原始指针unique_ptr析构函数是否也删除关联的原始指针?
- 为什么在将多态行为与指向接口的指针一起使用时没有调用析构函数?
- 在析构函数内取消引用指针时出现分段错误
- 共享指针析构函数中的内存顺序
- 在共享指针的值中调用 std::swap 调用一堆构造函数和析构函数
- 在特殊情况下使析构函数不是虚拟的,并删除基指针是否安全
- 删除对象而不调用成员指针的析构函数
- 析构函数在与 STL 的共享指针中调用两次