Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
658 views
in Technique[技术] by (71.8m points)

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

I have a custom QGraphicsItem, that (among other things) changed the cursor to an open hand when clicked, using the standard procedure as described in the Qt documentation. This worked fine for the past two weeks or so. Yesterday, I changed a few things inside the class. Most significantly, I now subclass directly from QGraphicsPixmapItem instead of QGraphicsItem.

At some point I started to get the following error (partly my own translation):

C664: Conversion of parameter 1 from 'Qt::CursorShape' to 'const QCursor &' not possible. Source or target has incomplete type.

I now try to figure out why my item has an incomplete type. Unfortunately I can't retrace when exactly this problem started to occur. The changed base class was my only guess, but I could not really think of a way how this could be the cause. After googling the error, I found examples, where members of the class were somewhat ill-defined, but I could not find an error in my declarations. So, here is the Header, in case I missed something:

#include <QGraphicsPixmapItem>
class Collection;
class ItemSource;

class PhotoItem : public QGraphicsPixmapItem
{
public:
    PhotoItem( QString sourceFilePath, Collection *collection =0, QColor color =Qt::white, qreal size = 80.0);

    enum Orientation    { portrait, landscape };

    QPixmap content();
    bool hasContent();
    QColor color();

    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
    QRectF boundingRect() const;

    void mousePressEvent(QGraphicsSceneMouseEvent *event);
    void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
    void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
    void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event);

private:
    qreal _size;
    Orientation _orientation;
    QPixmap _content;
    QColor _color;
    Collection *_collection;
    ItemSource *_source;
};

If this file is correct, then are there any common problems that lead to undefined types that I can check out? Maybe I am even searching at the wrong place?

Thanks for your time, Louise

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...