首页 > 解决方案 > Qt GUI C++ 全局声明错误

问题描述

我在添加全局变量时遇到问题。我尝试在一个单独的头文件中声明它们,称为“declare_struct.h”我将 .h 包含在 .cpp 中,但仍然无法引用变量,当我尝试在函数中使用它们时,我不断收到错误消息在头文件中。

这是标题中的内容:

#define DECLARE_STRUCT_H

#include <QString>
#include <QVector>
#include <QRegularExpression>
#include <QFile>
#include <QTextStream>



extern QString helper_quest, helper_a, helper_b, helper_c, helper_d, helper_good;

class Question
{
public:
    Question();public:
    QString quest, answer_a, answer_b, answer_c, answer_d, answer_good;
};

extern Question q;
extern QVector<Question> quiz;

void load (QString filename)
{
    quiz.clear();
    QRegularExpression re_Q("Q: ");
    QRegularExpression re_A("A: ");
    QRegularExpression re_B("B: ");
    QRegularExpression re_C("C: ");
    QRegularExpression re_D("D: ");
    QRegularExpression re_Ans("Ans: ");


    QString trash, need;
    QFile inf(filename);

  ///  if (inf.good())
   /// {
  ///      getline(inf, trash); ///deletes the title of the questionnaire
  ///  }

    for (int i =0; i<10; i++)
    {
        QTextStream in(&inf);

        q.quest = in.readLine();
        q.quest.remove(re_Q);

        q.answer_a = in.readLine();
        q.answer_a.remove(re_A);

        q.answer_b = in.readLine();
        q.answer_b.remove(re_B);

        q.answer_c = in.readLine();
        q.answer_c.remove(re_C);

        q.answer_d = in.readLine();
        q.answer_d.remove(re_D);

        q.answer_good = in.readLine();
        q.answer_good.remove(re_Ans);

        quiz.push_back(q);


    }
}

#endif // DECLARE_STRUCT_H    ```

in the declare_struct.cpp :
``` #include "declare_struct.h"

Question::Question()
{

}
```

And I keep getting the following errors:
[errors][1]


  [1]: https://i.stack.imgur.com/ZyerL.png

标签: c++qtvariablesheaderdeclaration

解决方案


推荐阅读