Search code examples
c++qtcompiler-errorsincludeheader-files

Error "Unterminated conditional directive" in cross-referencing headers


There are two classes that are related to each other in their headers:

PlotMarker

#ifndef PLOTMARKER_H
#define PLOTMARKER_H

#include <QObject>
#include "plotter.h"

class Plotter;

class PlotMarker : public QObject
{
    // ...
    Plotter* m_attachedPlot;    
    // ...
};

#endif // PLOTMARKER_H

Plotter

#ifndef PLOTTER_H
#define PLOTTER_H

// ...
#include "plotmarker.h"
// ...

class PlotMarker;

class Plotter : public QQuickPaintedItem
{
    // ...
    QLinkedList<PlotMarker*> m_markerList;
    // ...
};

#endif // PLOTTER_H

The program is compiled well, but it's got a error error: unterminated conditional directive in #ifndef and the code of classes in the IDE isn't highlighted because of it.

If I remove #include "plotter.h" in PlotMarker's header or #include "plotmarker.h" in Plotter's header, Qt Creator highlights the code as usual, but the compilating fails because of errors about invalid use of incomplete type.

Could you please tell me what's wrong? I think it's because of wrong headers cross-referencing, but I ran into this and it didn't help me.


Solution

  • The problem is solved.

    I just moved one of #include from the header to the source file, and it has worked.

    plotmarker.h

    #ifndef PLOTMARKER_H
    #define PLOTMARKER_H
    
    #include <QObject>
    
    class Plotter;
    
    class PlotMarker : public QObject
    {
        // ...
        Plotter* m_attachedPlot;    
        // ...
    };
    
    #endif // PLOTMARKER_H
    

    // ...

    plotmarker.cpp

    #include "plotmarker.h"
    #include "plotter.h"
    // ...