【问题标题】:QHash of QVectorsQVectors 的 QHash
【发布时间】:2013-08-17 16:58:37
【问题描述】:

我有一个QHash<QString, QVector<float> > qhash,并试图覆盖QVector 中的值,如下所示:

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: 错误:将 'const QVector' 作为 'void QVector::replace(int, const T&) [with T = float] 的 'this' 参数传递' 丢弃限定符 [-fpermissive]

谁能帮我解决这个问题?

【问题讨论】:

    标签: qt replace constants qhash qvector


    【解决方案1】:

    错误消息告诉您,您正试图在const 实例上使用非const 方法。在这种情况下,您尝试在const QVector 上调用QVector::replace。这主要是因为您使用的是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

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多