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'

4

2 回答 2

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
于 2013-08-16T10:02:29.337 回答
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

于 2013-08-16T10:03:59.523 回答