could somebody please explain const to me?
sorry for posting what i am afraid is a pure c++-problem here, but at least it is about a qt-program:
i have a loop going through a query and a QMap where i want to store the type and name of each column that is found. so i am trying to put the result of QSqlField::type() into my QMap, but this gives an error because it is const. Why is that? after all i had expected i am actually only copying the value into my QMap so no need to care about the original being const?
the code looks like this:
Code:
while (query.next()) {
for (int col=0;col<rec.count();col++) {
if (row == 0) {
QMap<QString,QVariant>* currCol = &columns.value(col);
currCol->insert("type",field.type()); // first error
currCol->insert("name",field.name()); // second error
}
qDebug() << "working on row " << row << ", column " << col;
}
tableData.value(row) = rec; //third error
row++;
}
and in the .h i have:
Code:
QMap<int,QSqlRecord> tableData;
QMap<int,QMap<QString,QVariant> > columns;
the errors i am getting are all like this:
Quote:
passing `const QString' as `this' argument of `QString& QString::operator=(char)' discards qualifiers
Re: could somebody please explain const to me?
Please post the exact error messages and tell us which lines they correspond to.
Re: could somebody please explain const to me?
Quote:
Originally Posted by
mikro
Code:
QMap<QString,QVariant>* currCol = &columns.value(col);
currCol->insert("type",field.type()); // first error
currCol->insert("name",field.name()); // second error
According to trolltech's docs value() method of QMap class returns const value:
Code:
const T value ( const Key & key ) const
so you cannot assign address of this value to pointer to non-const objects.
EDIT:
There is also another problem. What value() returns is temporary object. It's not advisable to take and use address of temporary object.
BTW: "Edit" button doesn't work under Opera.
Re: could somebody please explain const to me?
yes, that was the problem - damn... i had that one before. the problem was, that at first the lines were
Code:
columns.value(col).value("type") = field.type();
columns.value(col).value("name") = field.name();
and since i splitt that a little to make it better readable i didn't look at the line above anymore, especially as i understood the errormessage to be complaining about field.type() being a const QString not about the part on the left side being const. obviously this is also the problem with the third error. Thanks
Re: could somebody please explain const to me?
nothing specific to the problem but as an explaination of 'const', (among many other things)
http://www.parashift.com/c++-faq-lite/
K