嵌套的 QMap 和 QList 不允许我追加/推回
我正在尝试利用嵌套的 QList:
QMap<int, QMap<QString, QList<int> > > teamGames;
for (int team1 = 1; team1 <= TOTAL_TEAMS; ++team1) {
QMap<QString,QList<int>> games;
teamGames[team1]=games;
QList<int> home;
QList<int> away;
games["home"] = home;
games["away"] = away;
}
teamGames.value(1).value("home").push_back(1);
当我编译时,我得到: 1>.\main.cpp(154) : 错误 C2662: 'QList::push_back' : 无法将 'this' 指针从 'const QList' 转换为 'QList &'
我确信我忽略了一些简单的事情,或者也许有一个更简单的解决方案让我困惑。非常感谢任何帮助。
I am trying to utilize a nested QList:
QMap<int, QMap<QString, QList<int> > > teamGames;
for (int team1 = 1; team1 <= TOTAL_TEAMS; ++team1) {
QMap<QString,QList<int>> games;
teamGames[team1]=games;
QList<int> home;
QList<int> away;
games["home"] = home;
games["away"] = away;
}
teamGames.value(1).value("home").push_back(1);
When I compile I get:
1>.\main.cpp(154) : error C2662: 'QList::push_back' : cannot convert 'this' pointer from 'const QList' to 'QList &'
I'm sure its something simple that I'm overlooking, or maybe there is a simpler solution that's eluding me. Any help greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如您在此处
QMap::value( const Key & key) const;
返回一个const T
,这意味着您无法修改您得到的内容。即使可以,您也会修改放入地图中的值的副本。您需要的是 T& QMap::operator[](const Key& key) 返回与键关联的值作为可修改的引用。所以打电话As you can see here
QMap::value(const Key & key) const;
returns aconst T
, which means you can not modify what you get. Even if you could you would modify a copy of the value you put into the map. What you need is T& QMap::operator[](const Key& key) which returns the value associated with the key as a modifiable reference. So call