3

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.

gollumullog
  • 1,249
  • 2
  • 14
  • 22

1 Answers1

9

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);
Bill
  • 11,595
  • 6
  • 44
  • 52
  • 2
    well, maybe i answer too quickly, but as you stated in your answer, `QMap::value()` returns a const-copy-thing, so even for the first call you'll have to use `operator []` this way : `teamGames[1]["home"].push_back(1) ;`. Or am I wrong ? – azf Feb 02 '12 at 14:57
  • @totem I'm afraid you are right! I am going to edit my answer. Thank you! – Bill Feb 02 '12 at 15:15