将 .ui 文件导入 Qt5 C++时出错"was not declared in this scope"

Error "was not declared in this scope" when importing .ui file into Qt5 C++

本文关键字:not was declared in scope this 出错 文件 ui 导入 Qt5      更新时间:2023-10-16

我遇到错误"未在此范围内声明"时,将Qt Wizard从Qt Designer导入Qt C++应用程序。

src/main.cpp

#include <QApplication>
#include <stdlib.h>
#include <iostream>
#include "new_conference_wizard.h"

int main(int argc, char *argv[]) {
    QApplication a(argc, argv);
    NewConferenceWizard *conf = new NewConferenceWizard;
    conf->show();
    return a.exec();
}

src/new_conference_wizard.cpp

#include "new_conference_wizard.h"
NewConferenceWizard::NewConferenceWizard(QWidget *parent) : QMainWindow(parent), ui(new Ui::NewConferenceWizard) {
    ui->setupUi(this);
}
NewConferenceWizard::~NewConferenceWizard()
{
    delete ui;
}

src/new_conference_wizard.h

#ifdef NEW_CONFERENCE_WIZARD_H
#define NEW_CONFERENCE_WIZARD_H
#include <QWizard>
#include "ui_new_conference_wizard.h"
namespace Ui {
class NewConferenceWizard;
}
class NewConferenceWizard : public QWizard
{
    Q_OBJECT
public:
    explicit NewConferenceWizard(QWidget *parent = 0);
    ~NewConferenceWizard();
private:
    Ui::Widget *ui;
};

#endif

源代码中的所有文件(我只保留重要的文件进行调试(:https://drive.google.com/open?id=1rR4H9Na3bCvdyREn_GZjoDUs9lc3ieYi

编译器输出:

...
/mnt/DATA/Sync2/WorkData/HUST2019/Projects/NetworkAndMedia/Project/desktop-app/src/main.cpp: In function ‘int main(int, char**)’:
/mnt/DATA/Sync2/WorkData/HUST2019/Projects/NetworkAndMedia/Project/desktop-app/src/main.cpp:36:5: error: ‘NewConferenceWizard’ was not declared in this scope
     NewConferenceWizard *conf = new NewConferenceWizard;
     ^~~~~~~~~~~~~~~~~~~
/mnt/DATA/Sync2/WorkData/HUST2019/Projects/NetworkAndMedia/Project/desktop-app/src/main.cpp:36:26: error: ‘conf’ was not declared in this scope
     NewConferenceWizard *conf = new NewConferenceWizard;
                          ^~~~
/mnt/DATA/Sync2/WorkData/HUST2019/Projects/NetworkAndMedia/Project/desktop-app/src/main.cpp:36:26: note: suggested alternative: ‘cosf’
     NewConferenceWizard *conf = new NewConferenceWizard;
                          ^~~~
                          cosf
/mnt/DATA/Sync2/WorkData/HUST2019/Projects/NetworkAndMedia/Project/desktop-app/src/main.cpp:36:37: error: ‘NewConferenceWizard’ does not name a type
     NewConferenceWizard *conf = new NewConferenceWizard;

在你的头文件中,#ifdef NEW_CONFERENCE_WIZARD_H应该是相反的条件:

#ifndef NEW_CONFERENCE_WIZARD_H

对于您的拼写错误,文件内容永远不会包含在编译中,因此main.c那里没有声明。