我使用嵌套的QVariantMap,并且在定义采用路径(字符串列表)的方法时遇到问题,并将指针返回到低级QVariantMaps:
QVariantMap * getQVariantMap( QStringList spec) const {
QVariantMap * deep_p = this->hash_p;
for( QStringList::const_iterator it = spec.begin();
it!= spec.end();
++it)
{
QVariantMap::const_iterator i = hash_p->find( *it);
if( i == hash_p->end())
return 0x00;
res_p = & i.value().toMap();
}
return res_p;
}
...
QVariantMap map, level1, level2;
level2["L2"] = "bazzinga!";
level1["L1"] = QVariant::fromValue( level2);
level1["foo"] = "foo";
map["L0"] = QVariant::fromValue( level1);
map["foo"] = "foo";
QStringList qsl;
qsl.append( "L1");
qsl.append( "L0");
QVariantMap * qvm = getQVariantMap( qsl);//pointer to level2 part of the nested QVariantMap.
qvm[ "foo"] = "baz";
//map[ "L0"].toMap()[ "L1"].toMap()[ "foo"] == "baz";
...
我收到以下错误:
taking address of temporary [-fpermissive]
和我知道为什么。问题是我需要获得指向嵌套结构部分的指针,但我无法引用QVariant 的内部。我尝试使用post Assigning to nested QVariantMap中建议的data_ptr,但没有运气。由于显而易见的原因,我不想在插入/删除/值上做出特殊的包装(为什么,据我所知,在链接主题中接受的答案不是我的情况)。
我试过了:
某人应该如何正确地做到这一点?