PDA

View Full Version : how to define structure inside hash?



aurora
1st February 2012, 08:00
Hi is it possible to define a hash function with a structure inside it?
I tried to define as shown below,....
But getting link error saying..."exited with code -1073741819"



class FilterColl : public QDialog
{
Q_OBJECT

public:
struct StructCheckedEntries {
QString filename;
QCheckBox *checkbox;
QComboBox *combobox;
QLineEdit *lineedit;
};
QHash<QString,StructCheckedEntries *> HCheckedEntries;


private:
Ui::FilterColl *ui;
StructCheckedEntries *structentry;
};





Please tell me whats the mistake here?

wysota
1st February 2012, 08:10
Yes, it's possible. Is it really a linker error (meaning that the linker crashes) or rather an execution error (meaning that your program crashes)?

aurora
1st February 2012, 08:27
Yes, it's possible. Is it really a linker error (meaning that the linker crashes) or rather an execution error (meaning that your program crashes)?
When i ran the program.....nothing is displayed...evn mainwindow not opened....and after few seconds, got message as shown below....



The program has unexpectedly finished.
C:\Users\aurora\trial#3\singleFile-build-desktop-Qt_4_7_4_for_Desktop_-_MinGW_4_4__Qt_SDK__Debug\release\singleFile.exe exited with code -1073741819

wysota
1st February 2012, 08:30
Ok, so it is your program that is crashing and not that you are getting a "link error". Most likely you are referencing an uninitialized pointer somewhere in your code. This is strictly a C++ issue that has nothing to do with your Qt problem.

aurora
1st February 2012, 08:41
ok thank u wysota...
i will try to find out that....

desantossierra
1st February 2012, 08:51
I don't see any error in your declaration
is possible that there are something strange in your code?
could you copy some of it?

Jonny174
1st February 2012, 09:18
In *.h file:


struct StructCheckedEntries
{
StructCheckedEntries():
filename(""),
checkbox(0),
combobox(0),
lineedit(0)
{
checkbox = new QCheckBox;
combobox = new QComboBox;
lineedit = new QLineEdit;
}

inline void Free()
{
if (checkbox)
{
delete checkbox;
checkbox = 0;
}

if (combobox)
{
delete combobox;
combobox = 0;
}

if (lineedit)
{
delete lineedit;
lineedit = 0;
}
}

QString filename;
QCheckBox *checkbox;
QComboBox *combobox;
QLineEdit *lineedit;
};

QHash<QString,StructCheckedEntries *> HCheckedEntries;


In *.cpp file:


StructCheckedEntries entr;
entr.filename = "fileName";
entr.checkbox->setText("checkbox");
entr.combobox->addItem("combobox");
entr.lineedit->setText("lineedit");

HCheckedEntries.insert( "123", &entr );

StructCheckedEntries *pentr = HCheckedEntries.value("123");

qDebug() << "FileName: " << pentr->filename;
qDebug() << "ComboBox: " << pentr->combobox->itemText(0);
qDebug() << "LineEdit: " << pentr->lineedit->text();

if (pentr->checkbox->checkState() & Qt::Checked)
qDebug() << "Checked";
else
qDebug() << "Unchecked";


To delete use pentr->Free();

wysota
1st February 2012, 09:51
Do you initialize StructCheckedEntries and its checkbox, combobox and lineedit members anywhere?

Spitfire
1st February 2012, 10:21
I'm guessing you call Free() twice somewhere, and as you do not null the pointers you get crash when you try to delete already deleted pointer.
inline void Free()
{
if(checkbox)
{ // good coding practice: use brackets
delete checkbox;
checkbox = NULL; // good coding practice: always null deleted pointers
// do that otherwise the check above will always be true even after you've deleted the object
}

if (combobox)
{ // good coding practice: use brackets
delete combobox;
combobox= NULL; // good coding practice: always null deleted pointers
// do that otherwise the check above will always be true even after you've deleted the object
}
if (lineedit)
{ // good coding practice: use brackets
delete lineedit;
lineedit= NULL; // good coding practice: always null deleted pointers
// do that otherwise the check above will always be true even after you've deleted the object
}
}btw debugging the code should give you an answer as well.

aurora
1st February 2012, 15:15
Thank u all...:)
I did it....
Thank u so much....:)