Removing a pointer to an incomplete type of "Point"; no destructor called

I have 2 files:

Point.h :

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

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 following error:

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

any help appreciated!

+4
source share
3 answers

You need to add #include "Point.h" to your Line.h file. You can create and delete only full types.

Alternatively, delete the member function definitions from Line.h and put them in a separate Line.cpp file and include Point.h and Line.h in this file. This is a typical dependency reduction technique that makes code faster to compile, although with the potential loss of certain attachment capabilities.

+10
source

You indicated in the Point declaration that it is great for declaring a pointer or reference, but not for anything else in which the compiler needs to know the definition of the declared class.

If you need a forward declaration in the header file (right, just #include "Point.h" in Line.h ), then implement your Line functions in the implementation file, which is #include Point.h .

+3
source

To slightly expand the offer that other people have given, the line is always defined by two endpoints. There is not much point in defining these points as memory allocated by the heap. Why not make two points regular members of the Line class? This will save memory, improve performance, and lead to cleaner code. You must enable "Point.h" for this to work.

0
source

All Articles