Reputation: 21
In "main.cpp":
#include "mainwindow.h"
#include "mainwindow_1.h"
And I code in main.cpp:
MainWindow *mainWin_1 = new MainWindow;
MainWindow_1 *mainWin_2 = new MainWindow_1;
I'm already declared MainWindow
and MainWindow_1
in "mainwindow.h" and "mainwindow_1.h". They are both QMainWindow
. But when I debug, I got an error that said "MainWindow_1 was not declared in this scope".
When I changed:
#include "mainwindow.h"
#include "mainwindow_1.h"
into
#include "mainwindow_1.h"
#include "mainwindow.h"
I got the error "MainWindow was not declared in this scope".
Can I only include one mainwindow? How can I get two QMainwindow
in main.cpp without error?
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QDateTime>
#include <ctime>
class MainWindow : public QMainWindow {
Q_OBJECT;
public:
MainWindow();
~MainWindow();
};
#endif
mainwindow_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
Upvotes: 0
Views: 1047
Reputation: 62797
Sounds like you have same include guard macro in both .h files.
So, change both the #ifndef
and #define
near beginning of one of the .h files to be different from the include guards of the other .h file.
For example change mainwindow_1.h to have this:
#ifndef MAINWINDOW_1_H
#define MAINWINDOW_1_H
When you have same include guard macro, then the contents of the file included later will be skipped, and the class in it will be left undefined in that .cpp file.
One thing to keep in mind is, C++ include files are not like "import" or such of many other languages. #include
just inserts contents of the other file into the compilation, same as if you copy-pasted it. Among other things, this means that later include file "sees" all macros defined in earlier include files, so include guards must have unique names.
Upvotes: 1