سؤال

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.

هل كانت مفيدة؟

المحلول

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);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top