gpt4 book ai didi

c++ - Qt 错误 : Can't declared multiple mainwindows in main. cpp

转载 作者:行者123 更新时间:2023-11-28 06:36:20 25 4
gpt4 key购买 nike

在“main.cpp”中:

#include "mainwindow.h"
#include "mainwindow_1.h"

然后我在 main.cpp 中编写代码:

MainWindow *mainWin_1 = new MainWindow;
MainWindow_1 *mainWin_2 = new MainWindow_1;

我已经在“mainwindow.h”和“mainwindow_1.h”中声明了 MainWindowMainWindow_1。它们都是 QMainWindow。但是当我调试时,我收到一条错误消息,指出“MainWindow_1 未在此范围内声明”。

当我改变时:

 #include "mainwindow.h"
#include "mainwindow_1.h"

进入

 #include "mainwindow_1.h"
#include "mainwindow.h"

我收到错误消息“MainWindow 未在此范围内声明”。

我可以只包含一个主窗口吗?如何在 main.cpp 中得到两个 QMainwindow 而不会出错?

主窗口.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QDateTime>
#include <ctime>

class MainWindow : public QMainWindow {
Q_OBJECT;

public:
MainWindow();
~MainWindow();
};
#endif

主窗口_1.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QDateTime>
#include <ctime>

class MainWindow_1 : public QMainWindow {
Q_OBJECT;

public:
MainWindow_1();
~MainWindow_1();
};
#endif

最佳答案

听起来你在两个 .h 文件中都有相同的 include guard 宏。

因此,将其中一个 .h 文件开头附近的 #ifndef#define 更改为不同于另一个 .h 文件的包含防护。例如,将 mainwindow_1.h 更改为:

#ifndef MAINWINDOW_1_H
#define MAINWINDOW_1_H

当你有相同的include guard宏时,后面包含的文件内容将被跳过,其中的类将在那个.cpp文件中保持未定义。

要记住的一件事是,C++ 包含文件不像许多其他语言的“导入”或类似文件。 #include 只是将另一个文件的内容插入到编译中,就像您复制粘贴它一样。除其他事项外,这意味着后面的包含文件“看到”前面包含文件中定义的所有宏,因此包含守卫必须具有唯一的名称。

关于c++ - Qt 错误 : Can't declared multiple mainwindows in main. cpp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26721052/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com