将Qt向导从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;
答案 0 :(得分:1)
在头文件中,#ifdef NEW_CONFERENCE_WIZARD_H
应该处于相反的条件:
#ifndef NEW_CONFERENCE_WIZARD_H
使用拼写错误时,文件内容永远不会包含在编译中,因此main.c
那里没有声明。