我有一個具有向量作爲類成員的對象數組(實際上它是一個結構體)。該結構是:將矢量作爲類成員的對象數組
struct Cell {
vector<int> borderNodes;
vector<int> helperNodes;
int minNodePointer;
int maxNodePointer;
Cell() {
minNodePointer = -1;
maxNodePointer = -1;
}
Cell(int _minNodePointer, int _maxNodePointer) {
minNodePointer = _minNodePointer;
maxNodePointer = _maxNodePointer;
}
vector<int>& getHelperNodes() {
return helperNodes;
}
vector<int>& getBorderNodes() {
return borderNodes;
}
void setHelperNodesArray() {
sort(helperNodes.begin(), helperNodes.end());
}
void setBorderNodesArray() {
sort(borderNodes.begin(), borderNodes.end());
}
};
我已經建立這些對象的數組作爲一個全局變量有:
Cell* cells = new Cell[maxNumberOfCells];
,我想一些整數添加到該對象中的載體。
我想這(函數內部):
cells[cellId].borderNodes.push_back(node_id);
或
cells[cellId].getBorderNodes().push_back(node_id);
,他們編譯罰款,但沒有被添加到載體。如何正確的方法來做到這一點?這是從數據庫讀取的函數,並添加整數。查詢和從數據庫讀取是正確的,所以沒有錯誤。
void loadBorderNodesPerCellBnOnly(bool shouldRearrangeNodes, int subtractor, int maxNumberOfCells, int** cellBorderNodes) {
cellBorderNodes = new int*[maxNumberOfCells];
try {
work W(Conn);
for (int rownum = 0; rownum < r.size(); ++rownum) {
const result::tuple row = r[rownum];
vector<string> s = split(row[1].as<string >(), ' ');
const int mySize = s.size();
int cellId = row[0].as<int>();
cellBorderNodes[cellId] = new int[mySize];
for (int k = 0; k < mySize; k++) {
int node_id = atoi(s[k].c_str());
cellBorderNodes[cellId][k] = node_id;
(cells[cellId].getBorderNodes()).push_back(node_id);
try {
nodes[node_id - subtractor].cellId = cellId;
} catch (const std::exception &e) {
std::cerr << e.what() << std::endl;
}
nodes[node_id - subtractor].isBorderNode = true;
}
s.clear();
}
} catch (const std::exception &e) {
std::cerr << e.what() << std::endl;
}
for (int k = 0; k < maxNumberOfCells; k++) {
cout << k << ": " << cells[k].borderNodes.size() << endl;
cells[k].setBorderNodesArray();
if (maxBorderNodesPerCell < cells[k].borderNodes.size()) {
maxBorderNodesPerCell = cells[k].borderNodes.size();
}
}
}
你怎麼確定沒有添加任何東西? –
你怎麼知道什麼都沒有添加?對於(int k = 0; k
(cout << k <<「:」<< cells [k] .borderNodes.size()<< endl; } – Alexandros