2013-08-17 46 views
3

我有一個QHash<QString, QVector<float> > qhash,並試圖overwite在QVector值如下:QHash的QVectors

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.value().replace(j, new_value);行程說法得​​到一個錯誤如下:

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]

任何人都可以幫我解決這個問題?

回答

3

錯誤消息告訴您,您正試圖在const實例上使用非const方法。在這種情況下,您正嘗試撥打QVector::replace上的const QVector。造成這種情況的主要原因是因爲您使用的是QHashIterator,它只能從QHashIterator::value()返回const引用。

爲了解決這個問題,你可以在QHash使用STL風格的迭代,而不是Java風格的迭代器:

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); 
     } 
    } 
} 

你也可以使用QMutableHashIterator代替QHashIterator

+0

非常感謝您的回覆。我會盡快完成並留下反饋意見。 – SuTron

+0

感謝您的寶貴迴應。它爲我工作! – SuTron

+0

@MikaelEgibyan不客氣。不要忘記接受有用的答案:http://stackoverflow.com/help/accepted-answer :-) –