Solved error C2027: use of undefined type 'QMenuBar'
-
I'm reading a book in which there is a code as follows:
void MainWindow::createMenus() { fileMenu = menuBar()->addMenu(tr("&File")); fileMenu->addAction(newAction); fileMenu->addAction(openAction); ...
fileMenu
is defined asQMenu *fileMenu;
. I'm getting the following errormainwindow.cpp .\mainwindow.cpp(196): error C2027: use of undefined type 'QMenuBar' c:\qt\5.9.1\msvc2015\include\qtwidgets\qmainwindow.h(54): note: see declaration of 'QMenuBar'
I've taken a look at
menuBar()
in theQMainWindow
class which is defined asQMenuBar * menuBar() const
. Does that mean the code is old? Is there any change has been made for menuBar() from Qt4 to Qt5? Do I need to change the code to make it run? I'm Using Qt 5.9 with Visual Studio 2014. The author uses Qt 4. -
Hi @CroCo,
http://doc.qt.io/qt-5/qmenubar.html :
Header: #include <QMenuBar>
have you done this?
Regards
-
@aha_1980 said in error C2027: use of undefined type 'QMenuBar':
#include <QMenuBar>
that solved the problem. Thanks. How come a QMenu object is treated as a QMenuBar object?
-
-
@aha_1980 No fileMenu is QMenu pointer whereas the static function menuBar() returns a const QMenuBar pointer. I didn't do any casting.
-
@CroCo said in error C2027: use of undefined type 'QMenuBar':
whereas the static function menuBar() returns a const QMenuBar pointer.
That's what I said.
-
@aha_1980 I'm sorry I didn't get it. Could you please elaborate a bit.
-
Hi
it works because you assign the return of menuBar()->addMenu function call.
QMenu *addMenu(const QString &title); -
You already answered you question yourself. You have a
QMenuBar *menuBar
pointer that you want to de-reference. The compiler cannot do it if the declaration ofQMenuBar
is unknown.Therefore you have to add the
#include <QMenuBar>
before de-referencing. Ok? -
@aha_1980 thank you. it is clear.