PDA

View Full Version : error: expected class-name before '{' token



roar38
19th May 2010, 00:01
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!

spark
19th May 2010, 00:17
FORMS += sortdialog.ui
HEADERS += sortdialog.h \
SOURCES += main.cpp \
sortdialog.cpp \
main.cpp

That extra "\" in HEADERS is going to give you trouble. Remove it and see what happens:


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

SixDegrees
19th May 2010, 00:26
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.

norobro
19th May 2010, 00:43
You're exactly right SixDegrees. I was just looking at the "ui.h" file and the form name is "Dialog".

roar38
19th May 2010, 02:08
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

norobro
19th May 2010, 02:17
I changed
class SortDialog : public QDialog, public Ui::SortDialogto
class SortDialog : public QDialog, public Ui::Dialog and it compiled. Well, actually I had to comment out three lines in the SortDialog constructor to get it to compile:
SortDialog::SortDialog(QWidget *parent)
: QDialog(parent)
{
setupUi(this);
//secondaryGroupBox->hide();
//tertiaryGroupBox->hide();
//layout()->setSizeConstraint(QLayout::SetFixedSize);
setColumnRange('A', 'Z');
}So as I see it you can change the form name to SortDialog as SixDegrees suggested or change the line above.

roar38
19th May 2010, 02:22
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 :)

roar38
19th May 2010, 02:33
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?

norobro
19th May 2010, 02:48
What exactly are you trying to achieve?

roar38
19th May 2010, 03:24
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. :)

norobro
19th May 2010, 04:05
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

roar38
19th May 2010, 05:13
Can you show me how you did that in code?

Thanks

roar38
19th May 2010, 05:15
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.

norobro
19th May 2010, 05:34
Here's sortdialog.h:
class SortDialog : public QDialog, Ui::Dialog
{
Q_OBJECT

public:
SortDialog(QWidget *parent = 0);

void setColumnRange(QChar first, QChar last);

private slots:
void moreClicked(); // added a slot
};
And here's the constructor and the slot:
SortDialog::SortDialog(QWidget *parent)
: QDialog(parent)
{
setupUi(this);
setFixedSize(400,150);
buttonBox->setFixedSize(81, 100); // change size in your form
More->move(QPoint(290,55)); // move this pushbutton in your form
connect(More,SIGNAL(clicked()),this,SLOT(moreClick ed()));
setColumnRange('A', 'Z');
}

void SortDialog::moreClicked(){
setFixedSize(400,426); // resize dialog to show all group boxes using original size from ui_sortdialog.h
}
You can move "More" and change the size of the button box in your form and do away those two statements.