2

I've created the myClass class and in order to hide members, used d-pointer but got error while compiling my souce code. Here is the code:

Header file:

class myClassPrivate;

class myClass : public QObject
{
    Q_OBJECT
public:
    myClass(QObject *parent = 0);
    ~myClass();
    ...
signals:

public slots:

private:
    myClassPrivate *d;
};

and related .cpp

myClass::myClass(QObject *parent):
    QObject(parent),
    d(new myClassPrivate())
{
}

myClass::~myClass()
{
    delete d;
}

class myClassPrivate
{
  public:
    myClassPrivate();
    ...some methods...
    QTextStream stream;
    QFile* m_File;
};

myClassPrivate::myClassPrivate():
    m_File(new QFile)
{
}

It says: forward declaration of 'struct myClassPrivate'; invalid use of incomplete type 'myClassPrivate'

elgolondrino
  • 665
  • 9
  • 33

2 Answers2

4

You have to put your myClassPrivate declaration before using it in the myClass constructor. In the .cpp file:

class myClassPrivate
{
    // ...
};

myClass::myClass(QObject *parent):
    QObject(parent),
    d(new myClassPrivate())
{
}

You might want to check out some sources on the web explaining the concept and Qt's convenience macros Q_D, Q_DECLARE_PRIVATE and so on:

  1. Blog post on Qt private classes and D-pointers
  2. KDE Techbase on D-Pointers
Ferdinand Beyer
  • 64,979
  • 15
  • 154
  • 145
  • Well, thank you and can I create destructor in myClassPrivate in order to release (m_File), or there is no neccessity... – elgolondrino Aug 16 '13 at 10:10
  • Yes, you can and *should* create a `~myClassPrivate` destructor `delete`ing the `QFile` -- otherwise you would leak memory! – Ferdinand Beyer Aug 16 '13 at 10:14
3

Check my another answer, there are good sample that can be used as start point: Invalid use of incomplete type on qt private class

Community
  • 1
  • 1
Dmitry Sazonov
  • 8,801
  • 1
  • 35
  • 61