C.2. The Preprocessor: For #including Files

[ fromfile: preprocessor.xml id: preprocessor ]

Example C.2. src/preprocessor/constraintmap.h

#ifndef CONSTRAINTMAP_H
#define CONSTRAINTMAP_H

#include <QHash>
#include <QString>

class Constraint;                                           1

class ConstraintMap : public QHash<QString, Constraint*> {  2

private:
    Constraint* m_Constraintptr;                            3
    Constraint m_ConstraintObj;                             4
    void addConstraint(Constraint& c);                      5
};
#endif        //  #ifndef CONSTRAINTMAP_H

1

Forward declaration.

2

Needs definitions of QHash and QString, but only the declaration of Constraint, because it's a pointer.

3

No problem, it's just a pointer.

4

Error: incomplete type.

5

Using forward declaration.


Example C.3. src/preprocessor/constraintmap.cpp

#include "constraintmap.h"

ConstraintMap map;              1
#include "constraintmap.h"      2

Constraint* constraintP;        3

Constraint p;                   4
#include <constraint.h>
Constraint q;                   5

void ConstraintMap::addConstraint(Constraint& c) {
    cout << c.name();           6
}

1

Okay, ConstraintMap is already included.

2

Redundant but harmless if #ifndef wrapped.

3

Using forward declaration from constraintmap.h.

4

Error: incomplete type.

5

Now it is a complete type.

6

Complete type required here.




[91] The actual error message may not always be clear, and with QObjects, it might come from the MOC-generated code, rather than your own code.