我正在尝试循环包含unsigned int category
的对象的地图,我想将其设置为枚举值。 PlayerToon枚举值等于2.
地图宣言:
std::map<Action, Command> _actionBindings;
设置地图值:
//Assign category to value of '2'
for (auto actionPair : _actionBindings)
{
actionPair.second.category = Category::PlayerToon;
}
//Outputs '0', expected '2'
std::cout << "Category " << _actionBindings[Action::MoveLeft].category << "\n";
另一方面,如果我用手动做法明确地替换了循环,我的类别的值确实是预期的'2':
_actionBindings[Action::MoveLeft].category = Category::PlayerToon;
//Outputs '2'
std::cout << "Category " << _actionBindings[Action::MoveLeft].category << "\n";
答案 0 :(得分:6)
您正在制作地图元素的副本:
for (auto actionPair : _actionBindings)
改为使用引用:
for (auto& actionPair : _actionBindings)
^