0

我对 QList 有 const 正确性问题。

我有一个方法getValue,其签名我无法更改返回 const double 和这里

double vs = MinInput->getValue(0, 0);

vs 是常量。

我想用这种方法的结果构建 QList,我得到错误 C3892。

由于我的列表是 QList,因此无法添加 const double (?)

代码是这样的

    QList<double> minmax;
    for (int i = 0; i < 2*(3+othercutoffs_var_len) ; i++  )
        minmax.append( 0.0 );


    QSP< const VarInterface<double> > MinInput = ctx.getInputVar<double>(ctx.input(Id::fromString(QL1s("Min")))[0] );
    const double vs = MinInput->getValue(0, 0);
    minmax.at(0) = vs;

最后一行代码让我遇到了麻烦。(使用其他此类 const 双打填充列表时的其他错误)

getValue 的签名是这样的

const TYPE & VarData<TYPE>::getValue( uint r, uint c ) const
4

2 回答 2

3

我猜正确的代码是:

minmax[0] = vs;

更新:

QList::at返回const引用,不能修改。

于 2013-04-11T07:44:10.503 回答
2

QList::at(int i)是一个吸气剂函数。它返回一个const引用,你不能给它分配任何东西。

使用QList::operator[](int i)QList::replace(int i, const & T value)设置位置的值i

于 2013-04-11T07:48:56.430 回答