這裏是C++代碼。我很困惑,爲什麼解引用迭代器告訴我變量是隻讀的?它是Node類的公共成員。我的錯誤是什麼?C++代碼,只讀變量不可賦值
adjTable是一組節點的元素 - 見以下聲明的。
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'修改的其他SO信息http://stackoverflow.com/questions/908949/what-happens-when-you-modify-an-element-of-an-stdset – txtechhelp
此鏈接幫助我。我選擇使用const_cast((* pos).colour)= gray; –
pteeson