【发布时间】:2014-02-09 22:51:22
【问题描述】:
这是 C++ 代码。我很困惑为什么取消引用迭代器告诉我变量是只读的?它是 Node 类的公共成员。我的错误是什么?
adjTable 是一组 Node 元素 - 请参阅下面的声明。
Cells::iterator pos = adjTable.find(*thisNode);
if (pos == adjTable.end()) { // Did we find it?
NSLog(@"Not found");
// What to do here if node not found
}
// We found the node - mark it as grey in the table
(*pos).colour = grey; // <<<<<<<< this is the line with the issue
以下是声明等(格式似乎不正确)
class Node { // Define a single node a.k.a. matrix cell
public:
short nodeID; // the tag from the cell
short colour; // for tri-colour used in traversing
std::vector<short>adjs; // nodeIDs of adjacent nodes
// Ctors
Node(){};
Node(short ID, short col, std::vector<short>adjs)
: nodeID(ID), colour(col), adjs(adjs){}
// Dtors
~Node(){};
// operators
bool operator<(const Node& rhs) const{
return nodeID < rhs.nodeID;
}
bool operator==(const Node& rhs) const{
return nodeID == rhs.nodeID;
}
};
typedef std::set<Node,SortNodeSet> Cells;
class MakeTable {
public:
MakeTable(){};
~MakeTable(){};
Cells makeTable();
};
【问题讨论】:
-
关于
std::set修改stackoverflow.com/questions/908949/…的其他SO信息 -
这个链接帮助了我。我选择使用 const_cast
((*pos).colour) = gray;
标签: c++