cross-referencing headers 中的错误 "Unterminated conditional directive"

Error "Unterminated conditional directive" in cross-referencing headers

有两个 类 在它们的 header 中相互关联:

PlotMarker

#ifndef PLOTMARKER_H
#define PLOTMARKER_H

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

class Plotter;

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

#endif // PLOTMARKER_H

绘图仪

#ifndef PLOTTER_H
#define PLOTTER_H

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

class PlotMarker;

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

#endif // PLOTTER_H

程序编译正常,但在#ifndef中出现错误error: unterminated conditional directive,导致IDE中类的代码没有高亮显示.

如果我删除 PlotMarker 的 header 中的 #include "plotter.h" 或 Plotter 的 header 中的 #include "plotmarker.h",Qt Creator 照常高亮代码,但编译失败,因为有关错误不完整类型的无效使用。

你能告诉我哪里出了问题吗?我认为是因为 headers cross-referencing 错误,但我 运行 进入 this 并没有帮助我。

问题已解决。

我刚刚将 #include 中的一个从 header 移到了源文件中,它成功了。

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"
// ...

存在一个基本的设计缺陷。 例如

#include "b.h"

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

a.h上面的头文件

#include "a.h"

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

b.h上面的头文件

这是一个循环依赖

编译器将执行以下操作:

#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

我刚刚在 #ifndef 中获得了“unterminated conditional directive”,在 #end 中获得了“invalid preprocessing directive”。

我刚刚在 #end 之后添加了“if”(将“#end”编辑为“#endif”),该错误已修复。