Results 1 to 17 of 17

Thread: Expected class-name before '{' token

  1. #1
    Join Date
    Jun 2009
    Posts
    6
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Expected class-name before '{' token

    I've working on my first project and I've hit a speed bump. When building, I get this error message. Expected class-name before '{' token in mainwindow.h line 8.

    Here is mainwindow.h:

    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include "ui_mainwindow.h"
    5.  
    6.  
    7. class MainWindow : public QWidget, private Ui::MainWindowDLG
    8. {
    9. Q_OBJECT
    10.  
    11. public:
    12. MainWindow(QWidget *parent = 0);
    13.  
    14. public slots:
    15. void display_pic();
    16.  
    17. };
    18.  
    19. #endif
    To copy to clipboard, switch view to plain text mode 

    I know it's got to be something simple I am overlooking. Thanks in advance.

  2. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Expected class-name before '{' token

    add #include <QWidget> in this file, e.g.
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QWidget>
    5. #include "ui_mainwindow.h"
    6. ...
    To copy to clipboard, switch view to plain text mode 
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  3. #3
    Join Date
    Jun 2009
    Posts
    6
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: Expected class-name before '{' token

    Tried it, same result.

  4. #4
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Expected class-name before '{' token

    attach all sources.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  5. #5
    Join Date
    Jun 2009
    Posts
    6
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: Expected class-name before '{' token

    Attached #include <QWidget> too all other files main.cpp, mainwindow.cpp, and mainwindow.h. I think that is what you wanted me to try. Still get the same error message.

  6. #6
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Expected class-name before '{' token

    no, I was asking you to upload your sources, that I can try to compile your project by myself.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  7. #7
    Join Date
    Jun 2009
    Posts
    6
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: Expected class-name before '{' token

    Sorry. Here we go...

    main.cpp
    Qt Code:
    1. #include <QApplication>
    2. #include "mainwindow.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication app(argc, argv);
    7. MainWindow *dialog = new MainWindow;
    8.  
    9. dialog->show();
    10. return app.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 

    mainwindow.cpp
    Qt Code:
    1. #include "mainwindow.h"
    2. #include <QtGui>
    3. #include <QPixmap>
    4.  
    5.  
    6. MainWindow::MainWindow(QWidget *parent)
    7. {
    8. setupUi(this);
    9. connect( pushButton_display, SIGNAL( clicked() ), this, SLOT( display_pic() ) );
    10.  
    11. }
    12.  
    13. void MainWindow::display_pic()
    14. {
    15. QPixmap pixmap("Dima.jpg");
    16.  
    17. label_image->setPixmap(pixmap);
    18. }
    To copy to clipboard, switch view to plain text mode 

    mainwindow.h
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QWidget>
    5. #include "ui_mainwindow.h"
    6.  
    7.  
    8. class MainWindow : public QWidget, private Ui::MainWindowDLG
    9. {
    10. Q_OBJECT
    11.  
    12. public:
    13. MainWindow(QWidget *parent = 0);
    14.  
    15. public slots:
    16. void display_pic();
    17.  
    18. };
    19.  
    20. #endif
    To copy to clipboard, switch view to plain text mode 

    ui_mainwindow.h
    Qt Code:
    1. /********************************************************************************
    2. ** Form generated from reading ui file 'mainwindow.ui'
    3. **
    4. ** Created: Mon Jun 15 22:04:49 2009
    5. ** by: Qt User Interface Compiler version 4.5.1
    6. **
    7. ** WARNING! All changes made in this file will be lost when recompiling ui file!
    8. ********************************************************************************/
    9.  
    10. #ifndef UI_MAINWINDOW_H
    11. #define UI_MAINWINDOW_H
    12.  
    13. #include <QtCore/QVariant>
    14. #include <QtGui/QAction>
    15. #include <QtGui/QApplication>
    16. #include <QtGui/QButtonGroup>
    17. #include <QtGui/QHeaderView>
    18. #include <QtGui/QLabel>
    19. #include <QtGui/QMainWindow>
    20. #include <QtGui/QPushButton>
    21. #include <QtGui/QWidget>
    22.  
    23. QT_BEGIN_NAMESPACE
    24.  
    25. class Ui_MainWindow
    26. {
    27. public:
    28. QWidget *centralWidget;
    29. QLabel *label_image;
    30. QPushButton *pushButton_display;
    31.  
    32. void setupUi(QMainWindow *MainWindow)
    33. {
    34. if (MainWindow->objectName().isEmpty())
    35. MainWindow->setObjectName(QString::fromUtf8("MainWindow"));
    36. MainWindow->resize(770, 524);
    37. MainWindow->setStyleSheet(QString::fromUtf8("background-color: rgb(136, 162, 255);\n"
    38. ""));
    39. centralWidget = new QWidget(MainWindow);
    40. centralWidget->setObjectName(QString::fromUtf8("centralWidget"));
    41. label_image = new QLabel(centralWidget);
    42. label_image->setObjectName(QString::fromUtf8("label_image"));
    43. label_image->setGeometry(QRect(10, 10, 751, 461));
    44. label_image->setStyleSheet(QString::fromUtf8("background-color: rgba(62, 99, 255, 43);"));
    45. pushButton_display = new QPushButton(centralWidget);
    46. pushButton_display->setObjectName(QString::fromUtf8("pushButton_display"));
    47. pushButton_display->setGeometry(QRect(330, 480, 113, 32));
    48. MainWindow->setCentralWidget(centralWidget);
    49.  
    50. retranslateUi(MainWindow);
    51.  
    52. QMetaObject::connectSlotsByName(MainWindow);
    53. } // setupUi
    54.  
    55. void retranslateUi(QMainWindow *MainWindow)
    56. {
    57. MainWindow->setWindowTitle(QApplication::translate("MainWindow", "MainWindow", 0, QApplication::UnicodeUTF8));
    58. label_image->setText(QString());
    59. pushButton_display->setText(QApplication::translate("MainWindow", "Push Me", 0, QApplication::UnicodeUTF8));
    60. Q_UNUSED(MainWindow);
    61. } // retranslateUi
    62.  
    63. };
    64.  
    65. namespace Ui {
    66. class MainWindow: public Ui_MainWindow {};
    67. } // namespace Ui
    68.  
    69. QT_END_NAMESPACE
    70.  
    71. #endif // UI_MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

  8. #8
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Expected class-name before '{' token

    try this

    mainwindow.cpp
    Qt Code:
    1. #include "mainwindow.h"
    2. #include <QtGui>
    3. #include <QPixmap>
    4.  
    5.  
    6. MainWindow::MainWindow(QWidget *parent)
    7. : QMainWindow(this)
    8. {
    9. setupUi(this);
    10. connect( pushButton_display, SIGNAL( clicked() ), this, SLOT( display_pic() ) );
    11.  
    12. }
    13.  
    14. void MainWindow::display_pic()
    15. {
    16. QPixmap pixmap("Dima.jpg");
    17.  
    18. label_image->setPixmap(pixmap);
    19. }
    To copy to clipboard, switch view to plain text mode 

    mainwindow.h
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5. #include "ui_mainwindow.h"
    6.  
    7.  
    8. class MainWindow : public QMainWindow, private Ui::MainWindowDLG
    9. {
    10. Q_OBJECT
    11.  
    12. public:
    13. MainWindow(QWidget *parent = 0);
    14.  
    15. public slots:
    16. void display_pic();
    17.  
    18. };
    19.  
    20. #endif
    To copy to clipboard, switch view to plain text mode 
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  9. #9
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Expected class-name before '{' token

    Quote Originally Posted by spirit View Post
    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent)
    2. : QMainWindow(this)
    To copy to clipboard, switch view to plain text mode 
    Hi, a short side question. I pass everytime parent to the ctor of the base class. What's the advantage of passing this?

  10. #10
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Expected class-name before '{' token

    mistyped, of course must be passed parent.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Expected class-name before '{' token

    Did you add the "mainwindow.ui" file to your project file and reran qmake? I guess you did... nevermind...
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  12. #12
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Expected class-name before '{' token

    Quote Originally Posted by spirit View Post
    mistyped, of course must be passed parent.
    Fine, than I can stop thinking about

  13. #13
    Join Date
    Jun 2009
    Posts
    6
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: Expected class-name before '{' token

    Still not working.

  14. #14
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Expected class-name before '{' token

    ok, attach to your next post an archive with sources, pro-file and ui-file.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  15. #15
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Expected class-name before '{' token

    Are we blind or what?

    The class is called Ui::MainWindow, not Ui::MainWindowDLG!

    The line should say:
    Qt Code:
    1. class MainWindow : public QMainWindow, private Ui::MainWindow
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  16. #16
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Expected class-name before '{' token

    Quote Originally Posted by wysota View Post
    Are we blind or what?

    The class is called Ui::MainWindow, not Ui::MainWindowDLG!

    The line should say:
    Qt Code:
    1. class MainWindow : public QMainWindow, private Ui::MainWindow
    To copy to clipboard, switch view to plain text mode 
    damn, you are right!
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  17. #17
    Join Date
    Jun 2009
    Posts
    6
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: Expected class-name before '{' token

    That's it! Builds without any problems. Of course when I try to run the program I get an error message and the program doesn't start. "The application image_2 quit unexpectedly" And I got to ignore, report, or relaunch. I guess the fun is just beginning. Thanks for your help. I got a feeling I'll be back.

Similar Threads

  1. Replies: 3
    Last Post: 10th November 2008, 16:14
  2. Using flex with Qt4
    By xgoan in forum Qt Programming
    Replies: 4
    Last Post: 24th October 2008, 15:21
  3. expected initializer before »)« token
    By Holy in forum Qt Programming
    Replies: 2
    Last Post: 24th May 2008, 16:24
  4. Need help integrating sigc++ with a KDE3/Qt3 app
    By Valheru in forum Qt Programming
    Replies: 1
    Last Post: 17th November 2007, 15:39
  5. Compile Errors
    By luffy27 in forum Qt Programming
    Replies: 3
    Last Post: 4th November 2006, 05:26

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.