4

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.

mohsen
  • 1,763
  • 3
  • 17
  • 55
Nikita
  • 337
  • 1
  • 3
  • 12

3 Answers3

6

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"
// ...
Nikita
  • 337
  • 1
  • 3
  • 12
  • 1
    Thanks. Your answer guided me to reduce the includes. I used `namespaces` to solve the issue. – Maubeh Nov 24 '18 at 10:55
  • @Maubeh what do you mean you solve this with namespaces? – MNCODE Sep 16 '19 at 13:09
  • @MNCODE Sorry for the late reply. The following link helped me resolve the issue. http://www.cplusplus.com/forum/articles/10627/ – Maubeh Sep 20 '19 at 17:27
0

There is a fundamental design flaw. For example

#include "b.h"

class A
{
  B b;   // B is an object, can't be forward declared
};

a.h header file above

#include "a.h"

class B { 
         A* a // A is an object, can't be forward declared
};

b.h header file above

This is a Circular Dependencies

The compiler will do the following:

#include "a.h"

   // start compiling a.h
   #include "b.h"

      // start compiling b.h
      #include "a.h"

         // compilation of a.h skipped because it's guarded

      // resume compiling b.h
      class B { A* a };        // <--- ERROR, A is undeclared
blackmamba
  • 99
  • 3
  • 11
0

I've just gotten "unterminated conditional directive" in #ifndef and "invalid preprocessing directive" in #end.

I've just added "if" after #end(edit "#end" to "#endif"), that error is fixed.

Vittore Marcas
  • 1,007
  • 8
  • 11