PDA

View Full Version : QHash compile error



jiveaxe
27th August 2007, 13:09
Hi,
in a sample application I have created a class member like this:


QHash<int, QComboBox*> hash;
hash.insert(1, comboBox_1);
hash.insert(2, comboBox_2);
hash.insert(3, comboBox_3);

but during compilation I got the following error:


error: ISO C++ forbids declaration of ‘hash’ with no type
error: expected ‘;’ before ‘.’ token

for every insert function.

What's wrong?

Regards

marcel
27th August 2007, 13:16
You cannot use pointers as value types with these structures. Try using QPointers, but I am not sure if it will work.

EDIT: actually it works with pointers too. Could you provide more code, since the problem is there.


Regards

momesana
27th August 2007, 13:19
do you have included QHash's headerfile?
#include <QHash>
...
A mere forward declaration won't suffice in this case.

marcel
27th August 2007, 13:19
I think you forgot to include QHash in the cpp.

jiveaxe
27th August 2007, 13:24
But in the manual (http://doc.trolltech.com/4.3/qhash.html) there is an example concerning the use of contains() and value() and the operator[]() in which is created data like mine:


In general, we recommend that you use contains() and value() rather than operator[]() for looking up a key in a hash. The reason is that operator[]() silently inserts an item into the hash if no item exists with the same key (unless the hash is const). For example, the following code snippet will create 1000 items in memory:

// WRONG
QHash<int, QWidget *> hash;
...
for (int i = 0; i < 1000; ++i) {
if (hash[i] == okButton)
cout << "Found button at index " << i << endl;
}
To avoid this problem, replace hash[i] with hash.value(i) in the code above.


QHash is included.

jiveaxe
27th August 2007, 13:27
Here the header file:


#ifndef MAINDIALOG_H
#define MAINDIALOG_H

#include <QDialog>
#include <QHash>

class QComboBox;

class MainDialog: public QDialog
{
Q_OBJECT
public:
MainDialog(QWidget *parent=0);

private:
QComboBox *comboBox_1;
QComboBox *comboBox_2;
QComboBox *comboBox_3;

QHash<int, QComboBox*> hash;
hash.insert(1, comboBox_1);
hash.insert(2, comboBox_2);
hash.insert(3, comboBox_3);
};

#endif

marcel
27th August 2007, 13:43
hash.insert(1, comboBox_1);
hash.insert(2, comboBox_2);
hash.insert(3, comboBox_3);
But you cannot do this in the header, in the class declaration.
It has to be in a function.

Regards

jiveaxe
27th August 2007, 13:50
You're right; now the program compiles but there is a segmentation fault during execution; here the cpp file:


#include <QtGui>
#include "maindialog.h"

MainDialog::MainDialog(QWidget *parent)
: QDialog(parent)
{
hash.insert(1, comboBox_1);
hash.insert(2, comboBox_2);
hash.insert(3, comboBox_3);

comboBox_1 = new QComboBox;
comboBox_2 = new QComboBox;
comboBox_3 = new QComboBox;

for(int i = 1; i < 4; i++)
for(int j = 0; j < 4; j++)
hash.value(i)->addItem("New Entry");

QVBoxLayout mainLayout;
mainLayout.addWidget(comboBox_1);
mainLayout.addWidget(comboBox_2);
mainLayout.addWidget(comboBox_3);
}

The problem seems the line using hash member.

Bye

marcel
27th August 2007, 13:52
You have to instantiate the combos BEFORE adding them to the hash.

Regards

momesana
27th August 2007, 13:55
you also should create the layout with new instead of using an object that dies when it reaches the end of the scope of the constructor.

jiveaxe
27th August 2007, 13:58
Ops, you're again right.

Thanks