What leads to incomplete types? (QGraphicsItem: Source or target has incomplete type)

江枫思渺然 提交于 2019-11-29 12:38:42

What leads to incomplete types?

When instead of including the header file which defines a type, you add the line:

class myclass;

It Forward declares the class myclass which means for compiler it is an Incomplete type. With Incomplete types, One cannot create objects of it or do anything which needs the compiler to know the layout of myclass more than the fact that myclass is just an type. i.e: The compiler does not know what are its members and what its memory layout is. But Since pointers to all objects need just the same memory allocation, You can use the forward declaration when just reffering to an Incomplete type as a pointer.

So in short, You are forward declaring a class and then performing some operations which need the compiler to know the layout of that class, Since the compiler doesn't know it hence the error.

Forward declaration is usually fine in your headers, just make sure you include the class' header in your corresponding code file at the top (e.g. #include "itemsource.h").

In response to the edit: If I am sure I've included my own headers correctly and I'm still getting incomplete type errors, then the next step would be to change #include <QGraphicsPixmapItem> for the whole Qt package. Unless there is a clearly identifiable benefit I always favour including the package completely. It just makes sure the inner dependencies of that package are satisfied.

And finally, in Qt 4.x it appears QGraphicsPixmapItem is in QtGui but has been moved out to QtWidgets in 5.x. So if you're using 4 try replacing #include <QGraphicsPixmapItem> with #include <QtGui> and for 5 try #include <QtWidgets>.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!