嵌套的 QMap 和 QList 不允许我追加/推回

发布于 2025-01-01 09:46:23 字数 580 浏览 4 评论 0原文

我正在尝试利用嵌套的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

傲世九天 2025-01-08 09:46:23

正如您在此处 QMap::value( const Key & key) const; 返回一个 const T,这意味着您无法修改您得到的内容。即使可以,您也会修改放入地图中的值的副本。您需要的是 T& QMap::operator[](const Key& key) 返回与键关联的值作为可修改的引用。所以打电话

((teamGames[1])["home"]).push_back(1);

As you can see here QMap::value(const Key & key) const; returns a const 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

((teamGames[1])["home"]).push_back(1);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文