Results 1 to 10 of 10

Thread: QSharedDataPointer compile problem (invalid use of incomplete type)

  1. #1
    Join Date
    May 2006
    Posts
    70
    Thanks
    12
    Thanked 4 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QSharedDataPointer compile problem (invalid use of incomplete type)

    I'm trying to write a very simple program that makes use of QSharedDataPointer and QSharedData.

    Here is my class:
    Qt Code:
    1. #ifndef BUILDING_H
    2. #define BUILDING_H
    3.  
    4. #include <QSharedData>
    5. #include <QSharedDataPointer>
    6.  
    7. class BuildingPrivate;
    8.  
    9. class Building
    10. {
    11. public:
    12. Building();
    13. ~Building();
    14.  
    15. int value() const;
    16. void setValue(int value);
    17.  
    18. private:
    19. QSharedDataPointer<BuildingPrivate> d;
    20. };
    21.  
    22. #endif // BUILDING_H
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #include "building.h"
    2.  
    3. class BuildingPrivate : public QSharedData {
    4. public:
    5. BuildingPrivate() {
    6. value = 0;
    7. }
    8. BuildingPrivate(const BuildingPrivate &other) {
    9. value = other.value;
    10. }
    11. ~BuildingPrivate() {}
    12.  
    13. int value;
    14. };
    15.  
    16. Building::Building()
    17. : d(new BuildingPrivate)
    18. {
    19. }
    20.  
    21. Building::~Building() { }
    22.  
    23. int Building::value() const
    24. {
    25. return d->value;
    26. }
    27.  
    28. void Building::setValue(int value)
    29. {
    30. d->value = value;
    31. }
    To copy to clipboard, switch view to plain text mode 

    And here is my main.cpp:
    Qt Code:
    1. #include <QtCore/QCoreApplication>
    2. #include <QDebug>
    3.  
    4. #include "myobject.h"
    5.  
    6. Building build() {
    7. Building test;
    8. test.setValue(888);
    9. return test;
    10. }
    11.  
    12. int main(int argc, char *argv[])
    13. {
    14. QCoreApplication a(argc, argv);
    15.  
    16. qDebug() << build().value();
    17.  
    18. return 0;
    19. }
    To copy to clipboard, switch view to plain text mode 

    When I try to compile that I get:
    Qt Code:
    1. In file included from /usr/include/qt4/QtCore/QSharedData:2,
    2. from building.h:5,
    3. from myobject.h:6,
    4. from main.cpp:5:
    5. /usr/include/qt4/QtCore/qshareddata.h: In copy constructor ‘QSharedDataPointer<T>::QSharedDataPointer(const QSharedDataPointer<T>&) [with T = BuildingPrivate]’:
    6. building.h:10: instantiated from here
    7. /usr/include/qt4/QtCore/qshareddata.h:90: error: invalid use of incomplete type ‘struct BuildingPrivate’
    8. building.h:7: error: forward declaration of ‘struct BuildingPrivate’
    9. /usr/include/qt4/QtCore/qshareddata.h: In destructor ‘QSharedDataPointer<T>::~QSharedDataPointer() [with T = BuildingPrivate]’:
    10. building.h:10: instantiated from here
    11. /usr/include/qt4/QtCore/qshareddata.h:87: error: invalid use of incomplete type ‘struct BuildingPrivate’
    12. building.h:7: error: forward declaration of ‘struct BuildingPrivate’
    13. /usr/include/qt4/QtCore/qshareddata.h:87: warning: possible problem detected in invocation of delete operator:
    14. /usr/include/qt4/QtCore/qshareddata.h:87: warning: invalid use of incomplete type ‘struct BuildingPrivate’
    15. building.h:7: warning: forward declaration of ‘struct BuildingPrivate’
    16. /usr/include/qt4/QtCore/qshareddata.h:87: note: neither the destructor nor the class-specific operator delete will be called, even if they are declared when the class is defined.
    17. make: *** [main.o] Error 1
    To copy to clipboard, switch view to plain text mode 


    What am I missing?

  2. #2
    Join Date
    Oct 2008
    Posts
    71
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: QSharedDataPointer compile problem (invalid use of incomplete type)

    This member declaration:
    QSharedDataPointer<BuildingPrivate> d;
    Supposes a complete definition of BuildingPrivate - forward declaration wont work. Just move the definition of the BuildingPrivate to the header

  3. #3
    Join Date
    May 2006
    Posts
    70
    Thanks
    12
    Thanked 4 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSharedDataPointer compile problem (invalid use of incomplete type)

    Ahh ok I understand that now.

    But doesn't that kind of defeat the purpose of having a private implementation class? Plus the Qt documentation for QSharedDataPointer says the forward declaring is good enough. Any ideas?

  4. #4
    Join Date
    May 2006
    Posts
    70
    Thanks
    12
    Thanked 4 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSharedDataPointer compile problem (invalid use of incomplete type)

    (sorry double posted)
    Last edited by darkadept; 20th January 2010 at 03:53. Reason: double posted.

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,356
    Thanks
    3
    Thanked 5,010 Times in 4,789 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Blog Entries
    4
    Wiki edits
    10

    Default Re: QSharedDataPointer compile problem (invalid use of incomplete type)

    You need to have a copy constructor for the public class.

    Qt Code:
    1. Building(const Building &other) : d(other.d) {}
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. The following user says thank you to wysota for this useful post:

    darkadept (20th January 2010)

  7. #6
    Join Date
    May 2006
    Posts
    70
    Thanks
    12
    Thanked 4 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSharedDataPointer compile problem (invalid use of incomplete type)

    Yes you do! Thank you! I'm kicking myself now for not thinking about this problem properly. ;-)

    Also, remember to implement all the operators you might need, such as operator== or operator< etc.

    Here is my better and functioning Building class. IMHO this is a good template if you want to make a custom value class like QString that uses implicit sharing. In fact, go look at the QString source file to gain insight in building a good value class.

    Qt Code:
    1. #ifndef BUILDING_H
    2. #define BUILDING_H
    3.  
    4. #include <QSharedDataPointer>
    5. class BuildingPrivate;
    6.  
    7. class Building
    8. {
    9. public:
    10. Building(); //Constructor
    11. Building(const Building &other); //Copy-constructor
    12. ~Building(); //Destructor
    13.  
    14. Building &operator=(const Building &rhs); //Assignment operator
    15. bool operator==(const Building &other) const; //Equality operator
    16. inline bool operator!=(const Building &other) const { return !operator==(other); } //Inequality operator (efficiently reuses your equality operator)
    17.  
    18. //Your getters and setters
    19. int value() const;
    20. void setValue(int value);
    21.  
    22. private:
    23. QSharedDataPointer<BuildingPrivate> d;
    24. };
    25.  
    26. #endif // BUILDING_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include "building.h"
    2.  
    3. #include <QSharedData>
    4.  
    5. class BuildingPrivate : public QSharedData {
    6. public:
    7. BuildingPrivate() {
    8. value = 0;
    9. }
    10. BuildingPrivate(const BuildingPrivate &other)
    11. : QSharedData(other) {
    12. value = other.value;
    13. }
    14. ~BuildingPrivate() { }
    15.  
    16. int value;
    17. };
    18.  
    19. Building::Building()
    20. : d(new BuildingPrivate)
    21. { }
    22.  
    23. Building::Building(const Building &other)
    24. : d(other.d)
    25. { }
    26.  
    27. Building::~Building()
    28. { }
    29.  
    30. Building &Building::operator=(const Building &rhs)
    31. {
    32. if (this==&rhs) return *this; //Protect against self-assignment
    33. d = rhs.d;
    34. return *this;
    35. }
    36.  
    37. bool Building::operator==(const Building &other) const
    38. {
    39. /* If your value class has many data members you can get clever
    40. and compare the fastest values first (like comparing integers) and
    41. then move on to more cpu intensive compares. */
    42.  
    43. if (d->value == other.d->value)
    44. return true;
    45. return false;
    46. }
    47.  
    48. int Building::value() const
    49. {
    50. return d->value;
    51. }
    52.  
    53. void Building::setValue(int value)
    54. {
    55. d->value = value;
    56. }
    To copy to clipboard, switch view to plain text mode 

  8. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,356
    Thanks
    3
    Thanked 5,010 Times in 4,789 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Blog Entries
    4
    Wiki edits
    10

    Default Re: QSharedDataPointer compile problem (invalid use of incomplete type)

    Quote Originally Posted by darkadept View Post
    I'm kicking myself now for not thinking about this problem properly. ;-)
    It's right there in the docs, you know... Right after the sentence stating that forward declaration is enough
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #8
    Join Date
    Dec 2009
    Location
    Brisbane
    Posts
    17
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSharedDataPointer compile problem (invalid use of incomplete type)

    Quote Originally Posted by wysota View Post
    You need to have a copy constructor for the public class.

    Qt Code:
    1. Building(const Building &other) : d(other.d) {}
    To copy to clipboard, switch view to plain text mode 
    Is this in a FAQ somewhere? I work for QTDF, and even I've been caught by this one

  10. #9
    Join Date
    Jul 2010
    Posts
    21
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QSharedDataPointer compile problem (invalid use of incomplete type)

    Hi,

    I get the same compilation error with my classes, even though I have implemented the copy constructor. Here is the compiler output:

    Qt Code:
    1. [ 16%] Building CXX object CMakeFiles/test2.dir/AbstractInteractiveQGraphicsItem.cpp.o
    2. [ 33%] Building CXX object CMakeFiles/test2.dir/InteractiveQGraphicsLineItemData.cpp.o
    3. [ 50%] Building CXX object CMakeFiles/test2.dir/InteractiveQGraphicsLineItem.cpp.o
    4. [ 66%] Building CXX object CMakeFiles/test2.dir/main.cpp.o
    5. In file included from /usr/include/qt4/QtCore/QSharedData:1,
    6. from InteractiveQGraphicsLineItem.h:23,
    7. from main.cpp:2:
    8. /usr/include/qt4/QtCore/qshareddata.h: In copy constructor 'QSharedDataPointer<T>::QSharedDataPointer(const QSharedDataPointer<T>&) [with T = InteractiveQGraphicsLineItemData]':
    9. InteractiveQGraphicsLineItem.h:44: instantiated from here
    10. /usr/include/qt4/QtCore/qshareddata.h:93: error: invalid use of incomplete type 'struct InteractiveQGraphicsLineItemData'
    11. InteractiveQGraphicsLineItem.h:38: error: forward declaration of 'struct InteractiveQGraphicsLineItemData'
    12. /usr/include/qt4/QtCore/qshareddata.h: In destructor 'QSharedDataPointer<T>::~QSharedDataPointer() [with T = InteractiveQGraphicsLineItemData]':
    13. InteractiveQGraphicsLineItem.h:44: instantiated from here
    14. /usr/include/qt4/QtCore/qshareddata.h:90: error: invalid use of incomplete type 'struct InteractiveQGraphicsLineItemData'
    15. InteractiveQGraphicsLineItem.h:38: error: forward declaration of 'struct InteractiveQGraphicsLineItemData'
    16. /usr/include/qt4/QtCore/qshareddata.h:90: warning: possible problem detected in invocation of delete operator:
    17. /usr/include/qt4/QtCore/qshareddata.h:90: warning: invalid use of incomplete type 'struct InteractiveQGraphicsLineItemData'
    18. InteractiveQGraphicsLineItem.h:38: warning: forward declaration of 'struct InteractiveQGraphicsLineItemData'
    19. /usr/include/qt4/QtCore/qshareddata.h:90: note: neither the destructor nor the class-specific operator delete will be called, even if they are declared when the class is defined.
    20. make[2]: *** [CMakeFiles/test2.dir/main.cpp.o] Error 1
    21. make[1]: *** [CMakeFiles/test2.dir/all] Error 2
    22. make: *** [all] Error 2
    To copy to clipboard, switch view to plain text mode 

    The InteractiveQGraphicsLineItem object is declared as

    Qt Code:
    1. #ifndef INTERACTIVEQGRAPHICSLINEITEM_H
    2. #define INTERACTIVEQGRAPHICSLINEITEM_H
    3.  
    4. #include <QSharedData>
    5. #include "AbstractInteractiveQGraphicsItem.h"
    6.  
    7. class QRectF;
    8. class QColor;
    9. class QLineF;
    10. class QPainter;
    11. class QWidget;
    12. class QKeyEvent;
    13.  
    14. class InteractiveQGraphicsLineItemData;
    15.  
    16. class InteractiveQGraphicsLineItem : public AbstractInteractiveQGraphicsItem
    17. {
    18. public:
    19. InteractiveQGraphicsLineItem(AbstractInteractiveQGraphicsCoordinationItem* parent = 0, QGraphicsScene* scene = 0);
    20. InteractiveQGraphicsLineItem(const InteractiveQGraphicsLineItem & other) : d(other.d) {}
    21. virtual ~InteractiveQGraphicsLineItem() {}
    22.  
    23. virtual QRectF boundingRect() const;
    24. virtual QLineF getLine();
    25. virtual void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget = 0);
    26. virtual void setDrawNewRectOnClickOutOfTheFrame(bool b);
    27. virtual void setLine(const QLineF& line);
    28. virtual void setLineColor(const QColor &color);
    29. virtual void setHandleFillColor(const QColor &color);
    30. virtual void setHandleFrameColor(const QColor &color);
    31. virtual void setMaximumHandleSize(double size);
    32.  
    33. protected:
    34. virtual void adjustHandleSize();
    35. virtual void keyPressEvent(QKeyEvent* event);
    36. virtual void keyReleaseEvent(QKeyEvent* event);
    37. virtual void mousePressEvent(QGraphicsSceneMouseEvent* event);
    38. virtual void mouseMoveEvent(QGraphicsSceneMouseEvent* event);
    39. virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent* event);
    40.  
    41. virtual void updateHandles();
    42.  
    43. protected:
    44. QSharedDataPointer<InteractiveQGraphicsLineItemData> d;
    45. };
    46.  
    47. #endif // INTERACTIVEQGRAPHICSLINEITEM_H
    To copy to clipboard, switch view to plain text mode 

    The declaration of the data object looks like

    Qt Code:
    1. #ifndef INTERACTIVEQGRAPHICSLINEITEMDATA_H
    2. #define INTERACTIVEQGRAPHICSLINEITEMDATA_H
    3.  
    4. #include <QSharedData>
    5. #include <QRectF>
    6. #include <QLineF>
    7. #include <QPointF>
    8. #include <QVector>
    9. #include <QColor>
    10.  
    11. class InteractiveQGraphicsLineItemData : public QSharedData
    12. {
    13. public:
    14. InteractiveQGraphicsLineItemData();
    15. InteractiveQGraphicsLineItemData(const InteractiveQGraphicsLineItemData & other) {}
    16. ~InteractiveQGraphicsLineItemData() {}
    17. QLineF line;
    18. bool mouseDown;
    19. bool newSelection;
    20. bool drawNewLineOnClickOutOfTheFrame;
    21. double handleSize;
    22. double maximumHandleSize;
    23. QRectF* mouseOverHandle;
    24. QPointF dragStartPoint;
    25. QLineF lineBeforeDrag;
    26. QColor lineColor;
    27. QColor handleFillColor;
    28. QColor handleFrameColor;
    29.  
    30. QRectF LineHandle1, LineHandle2;
    31.  
    32. QVector<QRectF*> handles;
    33. };
    34.  
    35. #endif // INTERACTIVEQGRAPHICSLINEITEMDATA_H
    To copy to clipboard, switch view to plain text mode 

    and the main.cpp

    Qt Code:
    1. #include <QCoreApplication>
    2. #include "InteractiveQGraphicsLineItem.h"
    3.  
    4.  
    5. int main(int argc, char** argv)
    6. {
    7. QCoreApplication app(argc, argv);
    8. InteractiveQGraphicsLineItem foo;
    9. return app.exec();
    10. }
    To copy to clipboard, switch view to plain text mode 

    I think this is all the relevant information. Strange enough, I have a almost identical class which defines a Rect instead of a Line, and that class compiles fine even without any copy constructor. Can anyone help me?

  11. #10
    Join Date
    Jul 2010
    Posts
    21
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QSharedDataPointer compile problem (invalid use of incomplete type)

    for completeness, here is the InteractiveQGraphicsFrameItem class, which works without problems:

    Qt Code:
    1. #ifndef INTERACTIVEQGRAPHICSFRAMEITEM_H
    2. #define INTERACTIVEQGRAPHICSFRAMEITEM_H
    3.  
    4. #include <QSharedData>
    5. #include "GraphicItemsSharedLibrary.h"
    6. #include "AbstractInteractiveQGraphicsItem.h"
    7.  
    8. class QRectF;
    9. class QColor;
    10. class QPainter;
    11. class QWidget;
    12. class QKeyEvent;
    13.  
    14. class InteractiveQGraphicsFrameItemData;
    15.  
    16. class GRAPHICITEMS_SHAREDLIB_LIB_EXPORT InteractiveQGraphicsFrameItem : public AbstractInteractiveQGraphicsItem
    17. {
    18. public:
    19. InteractiveQGraphicsFrameItem(AbstractInteractiveQGraphicsCoordinationItem* parent = 0, QGraphicsScene* scene = 0);
    20. virtual ~InteractiveQGraphicsFrameItem();
    21. virtual QRectF boundingRect() const;
    22. virtual QRectF getRect();
    23. virtual void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget = 0);
    24. virtual void setDrawNewRectOnClickOutOfTheFrame(bool b);
    25. virtual void setFillColor(const QColor &color);
    26. virtual void setFrame(const QRectF& rect);
    27. virtual void setFrameColor(const QColor &color);
    28. virtual void setHandleFillColor(const QColor &color);
    29. virtual void setHandleFrameColor(const QColor &color);
    30. virtual void setMaximumHandleSize(double size);
    31. virtual void setOuterFillColor(const QColor &color);
    32.  
    33. protected:
    34. virtual void adjustHandleSize();
    35. virtual void keyPressEvent(QKeyEvent* event);
    36. virtual void keyReleaseEvent(QKeyEvent* event);
    37. virtual void mousePressEvent(QGraphicsSceneMouseEvent* event);
    38. virtual void mouseMoveEvent(QGraphicsSceneMouseEvent* event);
    39. virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent* event);
    40. // virtual void mouseDoubleClickEvent(QGraphicsSceneMouseEvent* event);
    41. // virtual void wheelEvent(QGraphicsSceneWheelEvent* event);
    42.  
    43. virtual void updateHandles();
    44.  
    45. protected:
    46. QSharedDataPointer<InteractiveQGraphicsFrameItemData> d;
    47. };
    48.  
    49. #endif // INTERACTIVEQGRAPHICSFRAMEITEM_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #ifndef INTERACTIVEQGRAPHICSFRAMEITEMDATA_H
    2. #define INTERACTIVEQGRAPHICSFRAMEITEMDATA_H
    3.  
    4. #include <QSharedData>
    5. #include <QRectF>
    6. #include <QPointF>
    7. #include <QVector>
    8. #include <QColor>
    9.  
    10. class InteractiveQGraphicsFrameItemData : public QSharedData
    11. {
    12. public:
    13. InteractiveQGraphicsFrameItemData();
    14.  
    15. QRectF frame;
    16. bool mouseDown;
    17. bool mouseInObject;
    18. bool newSelection;
    19. bool lockAspectRatio;
    20. bool drawNewRectOnClickOutOfTheFrame;
    21. double handleSize;
    22. double maximumHandleSize;
    23. QRectF* mouseOverHandle;
    24. QPointF dragStartPoint;
    25. QRectF frameBeforeDrag;
    26. QColor fillColor;
    27. QColor frameColor;
    28. QColor handleFillColor;
    29. QColor handleFrameColor;
    30. QColor outerFillColor;
    31.  
    32. // naming convention for handles
    33. // T top, B bottom, R Right, L left
    34. // 2 letters: a corner
    35. // 1 letter: the handle on the middle of the corresponding side
    36. QRectF TLHandle, TRHandle, BLHandle, BRHandle;
    37. QRectF LHandle, THandle, RHandle, BHandle;
    38.  
    39. QVector<QRectF*> handles;
    40. };
    41.  
    42. #endif // INTERACTIVEQGRAPHICSFRAMEITEMDATA_H
    To copy to clipboard, switch view to plain text mode 

    Now where is the difference to the InteractiveQGraphicsLineItem class, which makes the one compile and the other not?

Similar Threads

  1. incomplete type?
    By T0bi4s in forum General Programming
    Replies: 6
    Last Post: 11th February 2019, 07:02
  2. Replies: 4
    Last Post: 12th October 2009, 19:36
  3. Replies: 1
    Last Post: 4th September 2008, 14:55
  4. QApplication::desktop() incomplete type
    By codebehind in forum Newbie
    Replies: 7
    Last Post: 21st August 2008, 19:08
  5. Replies: 3
    Last Post: 4th August 2006, 12:05

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.