Results 1 to 14 of 14

Thread: error: expected class-name before '{' token

  1. #1
    Join Date
    May 2010
    Posts
    7
    Qt products
    Qt4
    Platforms
    MacOS X

    Cool error: expected class-name before '{' token

    Hello,

    I've recently completed the Qt tutorial - right out of the book - It's the "Shape Changing Dialogs" one, from chapter 2 "Creating Dialogs" and pages 29-35 of "C++ GUI program with Qt 4 1st edition".

    When I try to compile it, I get the above error, even though the class IS defined where it should be. Let me show you:

    /sortdialog.h
    #ifndef SORTDIALOG_H
    #define SORTDIALOG_H

    #include <QDialog>
    #include "ui_sortdialog.h"

    class SortDialog : public QDialog, public Ui::SortDialog
    {
    Q_OBJECT

    public:
    SortDialog(QWidget *parent = 0);

    void setColumnRange(QChar first, QChar last);
    };

    #endif // SORTDIALOG_H

    /sortdialog.cpp
    #include <QtGui>
    #include "sortdialog.h"

    SortDialog::SortDialog(QWidget *parent)
    : QDialog(parent)
    {
    setupUi(this);
    secondaryGroupBox->hide();
    tertiaryGroupBox->hide();
    layout()->setSizeConstraint(QLayout::SetFixedSize);
    setColumnRange('A', 'Z');
    }

    void SortDialog::setColumnRange(QChar first, QChar last)
    {
    primaryColumnCombo->clear();
    secondaryColumnCombo->clear();
    tertiaryColumnCombo->clear();

    secondaryColumnCombo->addItem(tr("None"));
    tertiaryColumnCombo->addItem(tr("None"));

    primaryColumnCombo->setMinimumSize(
    secondaryColumnCombo->sizeHint());

    QChar ch = first;
    while ( ch <= last ) {
    primaryColumnCombo->addItem(QString(ch));
    secondaryColumnCombo->addItem(QString(ch));
    tertiaryColumnCombo->addItem(QString(ch));
    ch = ch.unicode() + 1;
    }
    }

    /main.cpp
    #include <QApplication>
    #include "sortdialog.h"

    int main(int argc, char *argv[])
    {
    QApplication app(argc, argv);
    SortDialog *dialog = new SortDialog;
    dialog->setColumnRange('C', 'F');
    dialog->show();
    return app.exec();
    }

    /sortdialog.pro
    FORMS += sortdialog.ui
    HEADERS += sortdialog.h \
    SOURCES += main.cpp \
    sortdialog.cpp \
    main.cpp

    (note that sortdialog.ui is also available and correctly generates ui_sortdialog.h)

    As far as I know I'm doing everything as I should be. But why am I getting this error?

    Thanks for your help!
    Attached Files Attached Files

  2. #2
    Join Date
    May 2010
    Posts
    5
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: error: expected class-name before '{' token

    Qt Code:
    1. FORMS += sortdialog.ui
    2. HEADERS += sortdialog.h \
    3. SOURCES += main.cpp \
    4. sortdialog.cpp \
    5. main.cpp
    To copy to clipboard, switch view to plain text mode 
    That extra "\" in HEADERS is going to give you trouble. Remove it and see what happens:

    Qt Code:
    1. FORMS += sortdialog.ui
    2. HEADERS += sortdialog.h
    3. SOURCES += main.cpp \
    4. sortdialog.cpp \
    5. main.cpp
    To copy to clipboard, switch view to plain text mode 
    Last edited by spark; 19th May 2010 at 00:20. Reason: updated contents

  3. #3
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: error: expected class-name before '{' token

    I saw an error similar to this yesterday; I had named my form (based on your example) "sortDialog" but had used "SortDialog" in both the ui filename and in the inheritance declaration. Once I renamed the form "SortDialog" in the designer, all was well.

  4. #4
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: error: expected class-name before '{' token

    You're exactly right SixDegrees. I was just looking at the "ui.h" file and the form name is "Dialog".
    Last edited by norobro; 19th May 2010 at 03:03.

  5. #5
    Join Date
    May 2010
    Posts
    7
    Qt products
    Qt4
    Platforms
    MacOS X

    Unhappy Re: error: expected class-name before '{' token

    Hmm...that doesn't do it.

    The problem seems to be in this line, in sortdialog.h

    7 class SortDialog : public QDialog, public Ui::SortDialog

    if I leave it like that, then I get
    "/Users/ghost/Desktop/sort/sortdialog.h:8: error: expected class-name before '{' token" along with it claiming that alot of

    stuff known to be in ui_sortdialog.h (included in sortdialog.h) to be out of scope.

    If I comment out the second bit, like this
    class SortDialog : public QDialog // public Ui::SortDialog

    then it recognises the class definition, but the stuff in ui_sortdialog.h is still out of scope

  6. #6
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: error: expected class-name before '{' token

    I changed
    Qt Code:
    1. class SortDialog : public QDialog, public Ui::SortDialog
    To copy to clipboard, switch view to plain text mode 
    to
    Qt Code:
    1. class SortDialog : public QDialog, public Ui::Dialog
    To copy to clipboard, switch view to plain text mode 
    and it compiled. Well, actually I had to comment out three lines in the SortDialog constructor to get it to compile:
    Qt Code:
    1. SortDialog::SortDialog(QWidget *parent)
    2. : QDialog(parent)
    3. {
    4. setupUi(this);
    5. //secondaryGroupBox->hide();
    6. //tertiaryGroupBox->hide();
    7. //layout()->setSizeConstraint(QLayout::SetFixedSize);
    8. setColumnRange('A', 'Z');
    9. }
    To copy to clipboard, switch view to plain text mode 
    So as I see it you can change the form name to SortDialog as SixDegrees suggested or change the line above.

  7. #7
    Join Date
    May 2010
    Posts
    7
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: error: expected class-name before '{' token

    hah...when I fix it correctly (thanks SixDegrees!) I get a crash, with the following output from the debugger:

    Program received signal EXC_BAD_ACCESS, Could not access memory.
    Reason: KERN_PROTECTION_FAILURE at address: 0x00000004
    0x010ae229 in QLayout::setSizeConstraint ()

    Hmmm...again

    2010-05-19 04:18:58 +0300

    EXC_BAD_ACCESS (0x0001)
    KERN_PROTECTION_FAILURE (0x0002) at 0x00000004

    Thread 0 Crashed:
    0 QLayout::setSizeConstraint(QLayout::SizeConstraint ) + 9
    1 SortDialog::SortDialog[in-charge](QWidget*) + 183 (sortdialog.cpp:11)
    2 main + 74 (main.cpp:7)
    3 _start + 216
    4 start + 41

    Now when I comment out that line and compile, the example builds, and runs, but imperfectly.

    Well, better than nothing

  8. #8
    Join Date
    May 2010
    Posts
    7
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: error: expected class-name before '{' token

    HMMM

    when I change that line to this:

    layout()->setSizeConstraint(QLayout::SizeConstraint);

    i get this error from the compiler

    /Users/noone/Desktop/sort/sortdialog.cpp:10: error: expected primary-expression before ')' token

    any ideas?

  9. #9
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: error: expected class-name before '{' token

    What exactly are you trying to achieve?
    Attached Images Attached Images

  10. #10
    Join Date
    May 2010
    Posts
    7
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: error: expected class-name before '{' token

    when the dialog is launched, it should only be big enough to accomodate the first group box. When "more" is clicked, invoking the two lower group box, then the dialog rezises to accomodate them.

  11. #11
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: error: expected class-name before '{' token

    One way to do it is this: move the buttons so that they all show when your dialog shows only the primary key layout. Then connect the signal More::clicked() to a slot that resizes your dialog to show all of the layouts.
    I moved the More button in the constructor with move() since I don't have the SortDialog.ui file. I called setFixedSize() in the constructor for the small dialog and then called setFixedSize() again in the slot.
    Play around with your form to get the layout that you want.
    HTH
    Attached Images Attached Images

  12. #12
    Join Date
    May 2010
    Posts
    7
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: error: expected class-name before '{' token

    Can you show me how you did that in code?

    Thanks

  13. #13
    Join Date
    May 2010
    Posts
    7
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: error: expected class-name before '{' token

    Quote Originally Posted by roar38 View Post
    I called setFixedSize() in the constructor for the small dialog and then called setFixedSize() again in the slot.
    This is the part I meant - how you set up constructor and what the slot looked like.

  14. #14
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: error: expected class-name before '{' token

    Here's sortdialog.h:
    Qt Code:
    1. class SortDialog : public QDialog, Ui::Dialog
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. SortDialog(QWidget *parent = 0);
    7.  
    8. void setColumnRange(QChar first, QChar last);
    9.  
    10. private slots:
    11. void moreClicked(); // added a slot
    12. };
    To copy to clipboard, switch view to plain text mode 
    And here's the constructor and the slot:
    Qt Code:
    1. SortDialog::SortDialog(QWidget *parent)
    2. : QDialog(parent)
    3. {
    4. setupUi(this);
    5. setFixedSize(400,150);
    6. buttonBox->setFixedSize(81, 100); // change size in your form
    7. More->move(QPoint(290,55)); // move this pushbutton in your form
    8. connect(More,SIGNAL(clicked()),this,SLOT(moreClicked()));
    9. setColumnRange('A', 'Z');
    10. }
    11.  
    12. void SortDialog::moreClicked(){
    13. setFixedSize(400,426); // resize dialog to show all group boxes using original size from ui_sortdialog.h
    14. }
    To copy to clipboard, switch view to plain text mode 
    You can move "More" and change the size of the button box in your form and do away those two statements.

Similar Threads

  1. error: expected class-name before "{" token
    By freekill in forum Qt Programming
    Replies: 5
    Last Post: 9th January 2010, 16:34
  2. error: expected class-name before '{' token
    By Aresti in forum Qt Programming
    Replies: 1
    Last Post: 12th November 2008, 20:00
  3. Replies: 3
    Last Post: 10th November 2008, 16:14
  4. Replies: 4
    Last Post: 22nd February 2008, 19:08
  5. Replies: 2
    Last Post: 30th January 2008, 20:06

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.