我正在尝试遍历地图并为其中的元素分配值:
typedef map<int,Node, less<int> >::const_iterator TopologyIter;
bool
RoutingManager::ActivateNewNode()
{
TopologyIter iter;
do
{
if(!iter->second.online)
{
iter->second.online = true;
iter->second.connection = myConnection->newConnections.back();
myConnection->newConnections.pop_back();
return true; //all good
}
iter++;
}while (iter != topology.end());
return false; //received a connection, but no more nodes to hand out
}
但是,我收到以下错误:
manager.cpp:91:24:错误:在只读对象中分配成员'Node :: online' iter-&gt; second.online = true;
这是我的节点结构:
#pragma once
#include <map>
#include "nodecon.h"
typedef map<int,int, less<int> >::const_iterator NodeNeighborsIter;
struct Node
{
int id;
std::map<int,int> neighbors;
bool online;
struct NodeConnection connection;
};
我在这里缺少什么?
答案 0 :(得分:2)
const_iterator
不能用于修改容器的内容。通过解除引用const_iterator
获得的所有值均为const
。