Problem with inserting key, values into map

I have been having problems inserting pairs into my map. This is my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
void Team::CreateMatch(const matchInfo& arg_matchInfo, int week)
{
    if (week == -1)
    {
        week = matchList.size() + 1;
    }
    matchList.insert(pair<int, matchInfo>(week, arg_matchInfo));

    qDebug() << QString::fromStdString(arg_matchInfo.GetOpponentName());
    qDebug() << QString::fromStdString(matchList[week].GetOpponentName());

    matchList_output.append(QString::number(week) + QString("\t") + QString::fromStdString(arg_matchInfo.GetCorrectInfo()));
}



"Opponent name"
""
"Opponent name"
""
etc


(assume GetOpponentName() returns the value of a member called opponentName)

As you can see the first qDebug print is normal, as it should be, and the second qDebug should print the same, but it prints an empty QString. The insert function is somehow "erasing" the members's data of the matchInfo class. Any reasons why?
Last edited on
Perhaps matchInfo doesn't have the assignment operator properly defined?
It is true that I have not created my own assignment operator, but I do not believe I need one. matchInfo has no pointers (unless the string is), so shallow copying should work.
Have you actually put a break point and looked at the map after the insert to see what the Key/Value pair looks like inside the map?
Isn't that what my second qDebug() is doing? I am not sure what "break point" you are speaking of, though.
I do not know if this helps, but I believe it has something to do with the constructor of the matchInfo class. It seems to error when I do not provide the default argument (which is for opponentName and it is "").

That would be your exact problem. If you never change opponentName from "", then you're going to be creating pairs with an empty string.
That's what I thought as well. That seems awfully inconvenient way to do it then. Why must they construct the matchInfo class BEFORE I even insert anything?
The default constructor is only needed by operator[], even if it is never called.
However, you need a working copy constructor or move constructor.
Am I trying to copy arg_matchInfo into the matchInfo part in the pair class? If so, should my copy constructor be for matchInfo being assigned to another matchInfo?
Last edited on
Any reasons for the need of a copy constructor?
Yes. There are at least two copies, one when arg_matchInfo is copied into the pair and another one when it is copied from the pair into the map. The copy constructor is only required if there is no move constructor.

If so, should my copy constructor be for matchInfo being assigned to another matchInfo?

Not sure what you mean. matchInfo needs a copy constructor or a move constructor, that's all.
Last edited on
I added a copy constructor, but the result remained the same. This is what the copy constructor looks like:

1
2
3
4
matchInfo::matchInfo(const matchInfo & copyClass)
{
    opponentName = copyClass.GetOpponentName();
}


I did a little more research and found that it has to do with the insert function inserting the pair.

P.S. I solved the problem by just indexing and assigning, but it would be nice to know what caused this problem.
Last edited on
I did a little more research and found that it has to do with the insert function inserting the pair.


And what did you find, did you fix the problem? I do this everyday, inserting object into maps, and they copy everything just fine.

I am not sure what "break point" you are speaking of, though.

A break point is something you can enable/disable in a debugger at a given line of code. When running the program in debug mode, when that line is about to be executed the program stops execution at that point. You can then "step" over or in the code line by line. By doing this, after the insert into the map, you can then mouse over your map (or add to a watch list) and inspect the contents of your actual map. I'm surprised you are not familiar with break points, it's a crucial part of debugging imo.

Also, I would suggest using the member initialization list for all constructors/copy constructors where possible. Your resulting Ctor would look like:

1
2
3
matchInfo::matchInfo(const matchInfo & copyClass) : opponentName(copyClass.opponentName)
{
}

Last edited on
Haha, I never really used a debugger before, and have mostly done my own debugging with prints.

I probably got this habit because I came from a very, very basic game making program.
Unfortunately, the debugger cannot read the values in the map.
Can you copy and past the matchInfo class?

You could also have done, instead of insert.

matchList[week] = arg_matchInfo;

Which would have inserted or assigned if that week already existed in the map.
Last edited on
Yes, I figured that that solved the problem in this post:

http://cplusplus.com/forum/general/76000/#msg408177

I just wanted to know what was causing problems with the insert function.
It doesn't make sense that it would work like that but not with the insert, so it appears the assignment for the class is working as intended but not the copy constructor, which is why I was interested. Anyhow, good job.
Topic archived. No new replies allowed.