I have a QHash<QString, QVector<float> > qhash
,and trying to overwite the values in QVector
as following:
void Coordinate::normalizeHashElements(QHash<QString, QVector<float> > qhash)
{
string a = "Cluster";
float new_value;
float old_value;
const char *b = a.c_str();
float min = getMinHash(qhash);
float max = getMaxHash(qhash);
QHashIterator<QString, QVector<float> > i(qhash);
while (i.hasNext())
{
i.next();
if(i.key().operator !=(b))
{
for(int j = 0; j<i.value().size(); j++)
{
old_value = i.value().at(j);
new_value = (old_value - min)/(max-min)*(0.99-0.01) + 0.01;
i.value().replace(j, new_value);
}
}
}
}
I am getting an error on the i.value().replace(j, new_value);
stroke saying following :
C:\Qt\latest test\Prototype\Coordinate.cpp:266: error: passing 'const QVector' as 'this' argument of 'void QVector::replace(int, const T&) [with T = float]' discards qualifiers [-fpermissive]
could anyone help me to fix this?
The error message is telling you that you are attempting to use a non-const
method on a const
instance. In this case, you are trying to call QVector::replace
on a const QVector
. The main reason for this is because you are using QHashIterator
, which only returns const
references from QHashIterator::value()
.
To fix this, you could use the STL-style iterator instead of the Java-style iterator on the QHash
:
QString b("Cluster");
QHash<QString, QVector<float> >::iterator it;
for (it = qhash.begin(); it != qhash.end(); ++it)
{
if (it.key() != b)
{
for (int j=0; i<it.value().size(); j++)
{
old_value = it.value().at(j);
new_value = (old_value-min)/(max-min)*(0.99-0.01) + 0.01;
it.value().replace(j, new_value);
}
}
}
You could also use QMutableHashIterator
instead of QHashIterator
.