Error "Unterminated conditional directive" in cross-referencing headers

28,957

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"
// ...
Share:
28,957
Nikita
Author by

Nikita

Updated on January 20, 2022

Comments

  • Nikita
    Nikita over 2 years

    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.

  • Maubeh
    Maubeh over 5 years
    Thanks. Your answer guided me to reduce the includes. I used namespaces to solve the issue.
  • MNCODE
    MNCODE over 4 years
    @Maubeh what do you mean you solve this with namespaces?
  • Maubeh
    Maubeh over 4 years
    @MNCODE Sorry for the late reply. The following link helped me resolve the issue. cplusplus.com/forum/articles/10627
  • RShields
    RShields about 2 years
    Isn't the purpose of #ifndef and #define to prevent this?