c++ - 包含标题中的结构

标签 c++ qt

如何将在外部头文件中定义的 struct 包含到另一个文件中?起初,我通过指针传递结构,所以我只需要一个前向声明。但是现在想传值,编译不过。 header lagerverwaltung.h 确实包含该结构,那么问题出在哪里?

文件lagerverwaltung.h:

#ifndef LAGERVERWALTUNG_H
#define LAGERVERWALTUNG_H

struct etikettInfo{
    QString userName;
    QList<struct properties_content *> inhalte;
};

class Lagerverwaltung : public QMainWindow
{
    Q_OBJECT
    ... and so on
};
#endif // LAGERVERWALTUNG_H

文件etikett1.h:

#ifndef ETIKETT1_H
#define ETIKETT1_H

#include "lagerverwaltung.h" // for struct etikettInfo
class Lagerverwaltung;
//struct etikettInfo;

namespace Ui {
class Etikett1;
}

class Etikett1 : public QMainWindow
{
    Q_OBJECT
public:
    explicit Etikett1(Lagerverwaltung *mainwindow,
                      etikettInfo etikettData,  // <- struct etikettInfo has not been declared
                      QWidget* parent = 0);
    ~Etikett1();

private:
    Ui::Etikett1 *ui;
    Lagerverwaltung *lgrvw;
    etikettInfo etikettData;   // <- struct etikettInfo does not name a type
};

#endif // ETIKETT1_H

如果这很重要:两个 .h 文件都包含守卫。

好的,错误消息(请参阅代码中的注释)确实非常具有描述性,但我不知道我应该做些什么。

最佳答案

Also I have been including etikett1.h inside of lagerverwaltung.h

这是你的错误。您在 lagerverwaltung.h 中包含 etikett1.h,其中包含 etikett1.h,其中包含 lagerverwaltung.h 等等.换句话说,你有一个循环依赖。您的包含守卫会阻止无限包含,但也会导致其中一个 header 在另一个 header 之前被包含。在这种情况下,lagerverwaltung.h 首先包含在定义任何内容之前,特别是 etikettInfo - 它包含 etikett1.h... 这取决于在尚未定义的 etikettInfo 上。

如果 lagerverwaltung.h 确实依赖于 etikett1.h 并且 etikett1.h 唯一依赖于 lagerverwaltung.h etikettInfo 并且 lagerverwaltung.h 中没有任何内容依赖于 etikettInfo 那么最简单的解决方案是拆分 etikettInfo 的定义 到另一个 header 中,并将其包含在 etikett1.h 而不是 lagerverwaltung.h 中。

关于c++ - 包含标题中的结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30728960/

相关文章:

c# - 在 C# 中调用 c++ dll 函数时发生访问冲突

c++ - 显示 visual studio 代码 C++ 时出错

c++ - 通过 QT Creator 以 snake case 形式生成重构方法

c++ - 从 QML 调用多个 C++ 函数且第一个函数包含连接调用时,QML 在触发回调之前继续

c++ - 为什么自定义 QGraphicsItem 会导致场景或 View 移动,除非 boundingRect 为空?

c++ - 重载istream_iterator ----无法将左值绑定(bind)到 ‘std::basic_istream<char>&&’

c++ - Valgrind OpenCV

c++ - 在字节缓冲区中复制一个结构

c++ - Qt C++ 使用连接语句查找用法

python - Windows 8 不兼容?